您的位置:首页 > 其它

Quartz2D-06.利用贝瑟尔曲线画饼状图

2015-08-05 22:38 281 查看
效果图



代码实现

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: