11

基本上我需要一个带有不同颜色笔划的圆圈,大小都相同。例如,1/2 是蓝色,1/2 是红色。图片(抱歉图片太差了):

例子

我怎样才能画出这样的东西?

4

1 回答 1

23

有很多方法可以做到这一点,但一种是只绘制两条贝塞尔路径,每边一条:

- (void)drawRect:(CGRect)rect
{
    UIBezierPath *blueHalf = [UIBezierPath bezierPath];
    [blueHalf addArcWithCenter:CGPointMake(100, 100) radius:90.0 startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];
    [blueHalf setLineWidth:4.0];
    [[UIColor blueColor] setStroke];
    [blueHalf stroke];

    UIBezierPath *redHalf = [UIBezierPath bezierPath];
    [redHalf addArcWithCenter:CGPointMake(100, 100) radius:90.0 startAngle:M_PI_2 endAngle:-M_PI_2 clockwise:YES];
    [redHalf setLineWidth:4.0];
    [[UIColor redColor] setStroke];
    [redHalf stroke];
}

或者,如果您想在 Core Graphics 中执行此操作:

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

    CGContextSetLineWidth(context, 4);

    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
    CGContextAddArc(context, 100, 100, 90, -M_PI_2, M_PI_2, FALSE);
    CGContextStrokePath(context);

    CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextAddArc(context, 100, 100, 90, M_PI_2, -M_PI_2, FALSE);
    CGContextStrokePath(context);
}
于 2013-08-23T13:56:42.347 回答