2013-08-23 35 views
11

基本上我需要有一个不同颜色笔画的圆,大小相等。例如,1/2是蓝色,而1/2是红色。图片(对不起这么坏的形象):UIBezierPath不同笔画的画圈

Example

怎样绘制这样的事情?

+0

使用具有不同颜色的“UIBezierPath”绘制两个单独的弧线。 – Exploring

+0

是的,你可以做两个或两个以上的弧线,这取决于你需要的颜色数量。 – CodenameLambda1

回答

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

或者,如果你想这样做核芯显卡:

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

您可以通过使用'[[UIColor red/blueColor] setStroke]'而不是使用'CGContextSetStrokeColorWithColor(...)' – severin

+0

设置笔画颜色而不使用CG函数来达到同意。我修改了这个来说明两种技术。 – Rob