Drawing semicircles with CoreGraphics

I know how to draw lines and shapes with CGPath

. But I could not figure out how to draw this symbol for the electrical circuit

What code should you write to get a semi-circular shape?

This is what I have so far:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 5.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextMoveToPoint(context, 60, 80);
    CGContextAddLineToPoint(context, 120, 80);
    CGContextMoveToPoint(context, 120, 80);
    CGContextAddArc(120,80,M_PI,M_PI/2);
    CGContextMoveToPoint(context, 200, 80);
    CGContextAddLineToPoint(context, 300, 80);
    CGContextStrokePath(context);
}

      

+2


source to share


1 answer


You can create UIBezierPath

for example. something like:

UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint point = CGPointMake(0, 50);
CGFloat radius = 15.0;
CGFloat lineLength = 25.0;

[path moveToPoint:point];
point.x += lineLength;
[path addLineToPoint:point];
point.x += radius;
[path addArcWithCenter:point radius:radius startAngle:M_PI endAngle:M_PI * 2 clockwise:YES];
point.x += radius * 2;
[path addArcWithCenter:point radius:radius startAngle:M_PI endAngle:M_PI * 2 clockwise:YES];
point.x += radius * 2;
[path addArcWithCenter:point radius:radius startAngle:M_PI endAngle:M_PI * 2 clockwise:YES];
point.x += radius * 2;
[path addArcWithCenter:point radius:radius startAngle:M_PI endAngle:M_PI * 2 clockwise:YES];
point.x += lineLength + radius;
[path addLineToPoint:point];

      

Maybe your view controller will just create CAShapeLayer

and add it to your view layer

.

CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = [path CGPath];
layer.lineWidth = 2.0;
layer.fillColor = [[UIColor clearColor] CGColor];
layer.strokeColor = [[UIColor blackColor] CGColor];

[self.view.layer addSublayer:layer];

      



If you want to do it in a drawRect

subclass UIView

, you can, stroke

this way:

- (void)drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint point = CGPointMake(0, 50);
    CGFloat radius = 20.0;
    CGFloat lineLength = 45.0;

    [path moveToPoint:point];
    point.x += lineLength;
    [path addLineToPoint:point];
    point.x += radius;
    [path addArcWithCenter:point radius:radius startAngle:M_PI endAngle:M_PI * 2.0 clockwise:YES];
    point.x += radius * 2.0;
    [path addArcWithCenter:point radius:radius startAngle:M_PI endAngle:M_PI * 2.0 clockwise:YES];
    point.x += radius * 2.0;
    [path addArcWithCenter:point radius:radius startAngle:M_PI endAngle:M_PI * 2.0 clockwise:YES];
    point.x += radius * 2.0;
    [path addArcWithCenter:point radius:radius startAngle:M_PI endAngle:M_PI * 2.0 clockwise:YES];
    point.x += lineLength + radius;
    [path addLineToPoint:point];

    path.lineWidth = 2.0;
    [[UIColor blackColor] setStroke];
    [[UIColor clearColor] setFill];

    [path stroke];
}

      

Or, as your revised question suggests, if you're more comfortable with CoreGraphics, you can do this too:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGPoint point = CGPointMake(0, 50);
    CGFloat radius = 20.0;
    CGFloat lineLength = 45.0;

    CGContextMoveToPoint(context, point.x, point.y);
    point.x += lineLength;
    CGContextAddLineToPoint(context, point.x, point.y);
    point.x += radius;
    CGContextAddArc(context, point.x, point.y, radius, M_PI, M_PI * 2.0, NO);
    point.x += radius * 2.0;
    CGContextAddArc(context, point.x, point.y, radius, M_PI, M_PI * 2.0, NO);
    point.x += radius * 2.0;
    CGContextAddArc(context, point.x, point.y, radius, M_PI, M_PI * 2.0, NO);
    point.x += radius * 2.0;
    CGContextAddArc(context, point.x, point.y, radius, M_PI, M_PI * 2.0, NO);
    point.x += lineLength + radius;
    CGContextAddLineToPoint(context, point.x, point.y);

    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextSetLineWidth(context, 2.0);

    CGContextDrawPath(context, kCGPathStroke);
}

      

+6


source







All Articles