【问题标题】:Drawing half-circles with CoreGraphics使用 CoreGraphics 绘制半圆
【发布时间】:2013-11-17 20:10:12
【问题描述】:

我知道如何用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);
}

【问题讨论】:

  • 你在用CoreGraphics作图吗?请分享一些代码,以便我们有一些想法。
  • 您好,请不要删除其他人已经大量精力回答的问题。这是令人难以置信的自私,违背了这些网站的经营精神。

标签: ios drawing core-graphics


【解决方案1】:

您可以创建一个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];

如果您想在 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);
}

【讨论】:

  • 哦,太好了 :) 非常感谢。我只是想确定一些事情,难道没有更简单的方法可以在我的代码中添加 2 或 3 行吗?感谢您的帮助
  • @user2988343 你只是为每个驼峰做两行代码(CGContextAddArc 并调整point)。如果你愿意,你可以做for 循环......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 2016-03-07
  • 1970-01-01
  • 2022-11-29
  • 2011-04-13
相关资源
最近更新 更多