使用CoreGraphics绘制半圆 [英] Drawing half-circles with CoreGraphics

查看:158
本文介绍了使用CoreGraphics绘制半圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to draw lines and shapes with CGPaths . But I couldn't figure out how to draw this symbol for a circuit diagram

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

What code should I write to get that half circle shape?

这是我到目前为止:

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


推荐答案

你可以创建 UIBezierPath ,例如类似于:

You could create a UIBezierPath, e.g. 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];

你可以让你的视图控制器创建一个 CAShapeLayer 并将其添加到视图的图层

You could have your view controller just create a CAShapeLayer and add it to your view's 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 这条路径:

If you wanted to do it in the drawRect of a UIView subclass you could, stroke this path:

- (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更满意你也可以这样做:

Or, as your revised question suggests, if you're more comfortable with CoreGraphics, you could do that, 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);
}

这篇关于使用CoreGraphics绘制半圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆