ios - 使用 CoreGraphics 绘制半圆

标签 ios drawing core-graphics

我知道如何使用 CGPaths 绘制线条和形状。但是我不知道如何为电路图绘制这个符号

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

这是我目前所拥有的:

- (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,例如像这样的东西:

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

您可以让您的 View Controller 只创建一个 CAShapeLayer 并将其添加到您的 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];

如果您想在UIView 子类的drawRect 中执行此操作,您可以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);
}

关于ios - 使用 CoreGraphics 绘制半圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20035548/

相关文章:

python - 如何以编程方式创建具有动画方面的图片

iphone - UIView透明渐变

iphone - 如何在滚动到固定背景上方的 UIView 上使用 CGBlendMode?

ios - 使用 CGAffineTransform 旋转后 UIView 调整大小 View 大小是不可预测的

android - 应用程序中嵌入的youtube视频播放黑屏

c++ - 绘制从二值图像中检索到的轮廓

c++ - 单击按钮时在 QFrame 中绘制。

iphone - 核心显卡旋转路径

iphone - 按日期从 iPhone 中删除本地通知

ios - 如何从 Google GMS 地址获取 Google 地点 ID