您的位置:首页 > 其它

画饼图

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