2014-02-14 137 views
1

我预计很容易找到这个问题的答案,但不幸的是,我的搜索没有产生任何结果。如何在iOS中绘制这样的圈子?如何在iOS中绘制分段圆?

enter image description here

+3

多远没'CGPathAddArc(...)'(CoreGraphics中)或'addArcWithCenter:半径:由startAngle:endAngle:顺时针方向:'(上UIBezierPath)帮你吗? –

+0

@DavidRönnqvist谢谢你的提示。我不知道从哪里开始 –

+0

欢迎您。当你完成这个任务时,回过头来回答你自己的问题,以便其他有相同问题的人可以受益;) –

回答

3

我结束了使用UIBezierPath。这是我的画给定的循环:

CGFloat DegreesToRadians(CGFloat degrees) 
{ 
    return degrees * M_PI/180; 
}; 

- (void)drawRect:(CGRect)rect 
{ 
CGFloat radius = 70; 
CGPoint center = CGPointMake(90 + radius, 170 + radius); 
CGFloat start = DegreesToRadians(-90), end; 

NSArray *angles = @[@"0", @"60", @"-90"]; 
NSArray *colors = @[[UIColor yellowColor], [UIColor blueColor], [UIColor redColor]]; 

int col_counter = 0; 

for (NSString *angle in angles) 
{ 
    CGFloat value = [angle floatValue]; 
    end = DegreesToRadians(value); 

    CGPoint next; 
    next.x = center.x + radius * cos(start); 
    next.y = center.y + radius * sin(start); 

    UIBezierPath *path = [UIBezierPath bezierPath]; 

    [path moveToPoint:center]; 
    [path addLineToPoint:next]; 
    [path addArcWithCenter:center radius:radius startAngle:start endAngle:end clockwise:YES]; 
    [path addLineToPoint:center]; 

    UIColor *color = colors[col_counter]; 
    [color set]; 
    [path fill]; 

    col_counter++; 

    start = end; 
} 
} 
1

使用安德烈的回答,我能够在循环后添加一小段代码中添加一个白色的圆圈,中间要创造什么看起来像一个白色圆圈用厚实,多彩的边框。

enter image description here

end = DegreesToRadians(360); 

start = DegreesToRadians(-90); 

int width = 10; // thickness of the circumference 
radius -= width; 

CGPoint next; 
next.x = center.x + radius * cos(start); 
next.y = center.y + radius * sin(start); 

UIBezierPath *path = [UIBezierPath bezierPath]; 

[path moveToPoint:center]; 
[path addLineToPoint:next]; 
[path addArcWithCenter:center radius:radius startAngle:start endAngle:end clockwise:YES]; 
[path addLineToPoint:center]; 

UIColor *color = [UIColor whiteColor]; 
[color set]; 
[path fill];