- 效果图

- 代码实现
#import "ZJCakeView.h"
@implementation ZJCakeView
- (void)drawRect:(CGRect)rect {
CGFloat radius = self.bounds.size.width * 0.5;
CGPoint center = CGPointMake(radius, self.bounds.size.height * 0.5);
CGFloat statA = 0;
CGFloat angle = 0;
CGFloat endA = 0;
NSArray *data = @[@20,@20,@35,@25];
for (int i = 0; i < data.count; i++) {
int num = [data[i] intValue];
statA = endA;
angle = num/100.0 * M_PI * 2;
endA = statA + angle;
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:center radius:radius startAngle:statA endAngle:endA clockwise:YES];
[path addLineToPoint:center];
[[self randomColor] set];
[path fill];
}
}
-(UIColor *)randomColor{
CGFloat r = arc4random_uniform(256)/255.0;
CGFloat g = arc4random_uniform(256)/255.0;
CGFloat b = arc4random_uniform(256)/255.0;
return [UIColor colorWithRed:r green:g blue:b alpha:1];
}
@end