使用CoreGraphics绘制半圆

时间:2013-11-17 20:10:13

标签: ios drawing core-graphics

我知道如何用CGPath s绘制线条和形状。但我无法弄清楚如何为电路图绘制这个符号

我应该编写什么代码才能获得半圆形状?

这是我到目前为止所做的:

- (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);
}

1 个答案:

答案 0 :(得分:6)

您可以创建UIBezierPath,例如类似的东西:

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];

您可以让视图控制器创建CAShapeLayer并将其添加到视图的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];

如果您想在drawRect子类的UIView中执行此操作,stroke此路径:

- (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];
}

或者,正如您修改后的问题所暗示的那样,如果您对CoreGraphics更加满意,您也可以这样做:

- (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);
}