效果
重写 - (void)drawRect:(CGRect)rect { //... }
线宽,用线去涂色!
CGFloat viewHalfWidth = rect.size.width/2.0;
CGFloat lineWidth = viewHalfWidth;
内部想预留空间减去相应长度即可
CGFloat insideCircleWidth = 50;
lineWidth = viewHalfWidth - insideCircleWidth;
半径、中心点
CGFloat circleRadius = viewHalfWidth - lineWidth/2;
CGPoint circleCenterPoint = CGPointMake(viewHalfWidth, viewHalfWidth);
必要的数据
NSArray<UIColor*>*colorArray = [NSArray arrayWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor brownColor], [UIColor cyanColor], nil];
NSArray<NSNumber*>*percentArray = [NSArray arrayWithObjects:@(.1), @(.2), @(.3), @(.4), nil];
准备好之后开始画了!
CGContextRef pieViewContext = UIGraphicsGetCurrentContext();
CGFloat startAngel = 0;
CGFloat endAngel;
for (int i =0; i < colorArray.count; i++) {
endAngel = startAngel + M_PI*2*percentArray[i].floatValue;
CGContextAddArc(pieViewContext, circleCenterPoint.x, circleCenterPoint.y, circleRadius, startAngel, endAngel, NO);
CGContextSetStrokeColorWithColor(pieViewContext, colorArray[i].CGColor);
CGContextSetLineWidth(pieViewContext, lineWidth);
CGContextStrokePath(pieViewContext);
startAngel = endAngel;
}
最后,发挥自己的想象,抽离出其中变量,满足自己的具体需求!