#import "BinTu.h"
@implementation BinTu
- (void)drawRect:(CGRect)rect {
NSArray *array = @[@25,@25,@50];
for (NSNumber *num in array) {
[self drawPicture:num.floatValue andColour:[self randomColour]];
}
}
// 根据开始和结束角度画饼图
static CGFloat endA = 0;
- (void)drawPicture:(CGFloat)value andColour:(UIColor *)colour
{
CGPoint center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
CGFloat radius = self.bounds.size.width * 0.5 - 20;
CGFloat startA = endA;
CGFloat spaceA = value /100.0 * M_PI * 2;
endA = startA + spaceA;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
[path addLineToPoint:center];
[colour set];
[path fill];
}
// 创建随机颜色
- (UIColor *)randomColour
{
int numR = arc4random_uniform(256);
int numG = arc4random_uniform(256);
int numB = arc4random_uniform(256);
return [UIColor colorWithRed:numR/255.0 green:numG/255.0 blue:numB/255.0 alpha:1];
}
@end