#import "PieView.h"
@implementation PieView
- (void)drawRect:(CGRect)rect {
// Drawing code
NSArray *data = @[ @15, @13, @22, @5, @10, @35 ];
CGPoint center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
CGFloat radius = self.bounds.size.width * 0.5 - 10;
CGFloat startAngle = 0;
CGFloat endAngle = 0;
CGFloat angle = 0;
for (NSNumber *num in data) {
startAngle = endAngle;
angle = num.intValue / 100.0 * M_PI * 2;
endAngle = startAngle + angle;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path addLineToPoint:center];
[[self randomColor] setFill];
[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.0];
}
@end
效果图如下: