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

来源:互联网 发布:python try except 编辑:程序博客网 时间:2024/06/15 02:35
  • 效果图
    这里写图片描述
  • 代码实现
#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
0 0