画一个圆圈,用不同的颜色填充不同的部分

suj*_*jat 6 core-graphics quartz-2d ipad ios

我是ios的新手程序员.在我的一个项目中,我需要绘制一个圆圈,其中圆圈的不同部分将填充不同的颜色.我可以绘制圆圈.但我无法确定不同的部分圆圈,并用不同的颜色填充它们.这是一个截图,以澄清我想要绘制的内容.用不同颜色的圆圈

帮助将不胜感激.

Kar*_*arl 11

您可以使用UIBezierPath哪种方法addArcWithCenter:radius:startAngle:endAngle:clockwise:可以指定半径,中心和角度.代码看起来像这样,以绿色绘制四分之一圆:

CGPoint center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f, CGRectGetHeight(self.bounds) / 2.f);
CGFloat radius = center.x - 10.f;

UIBezierPath *portionPath = [UIBezierPath bezierPath];
[portionPath moveToPoint:center];
[portionPath addArcWithCenter:center radius:radius startAngle:0.f endAngle:M_PI_2 clockwise:YES];
[portionPath closePath];

[[UIColor greenColor] setFill];
[portionPath fill];

UIBezierPath *portionPath1 = [UIBezierPath bezierPath];
[portionPath1 moveToPoint:center];
[portionPath1 addArcWithCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
[portionPath1 closePath];

[[UIColor blueColor] setFill];
[portionPath1 fill];
Run Code Online (Sandbox Code Playgroud)

当然,您也可以考虑使用像CorePlot这样的库.