2014-02-14 173 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];