//
// PieView.m
#import "PieView.h"
@implementation PieView
- (void)drawRect:(CGRect)rect
{
// 数据
NSArray *arr = @[@25,@25,@50];
//属性值
CGFloat radius = rect.size.width * 0.5;
CGPoint center = CGPointMake(radius, radius);
CGFloat startAngle = 0;
CGFloat endAngle = 0;
CGFloat angle = 0;
for (int i=0; i<arr.count; i++) {
startAngle = endAngle;
angle = [arr[i] doubleValue] / 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 colorRandom] set];
[path fill];
}
}
- (UIColor *)colorRandom
{
CGFloat red = arc4random_uniform(256) / 255.0;
CGFloat green = arc4random_uniform(256) / 255.0;
CGFloat blue = arc4random_uniform(256) / 255.0;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
-(void)test
{
// 数据
NSArray *arr = @[@25,@25,@50];
//属性值
CGFloat radius = self.bounds.size.width * 0.5;
CGPoint center = CGPointMake(radius, radius);
CGFloat startAngle = 0;
CGFloat endAngle = 0;
CGFloat angle = 0;
// 第一个扇形
angle = 25 / 100.0 * M_PI * 2;
endAngle = startAngle + angle;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path addLineToPoint:center];
[[UIColor redColor] set];
[path fill];
// 第2个扇形
startAngle = endAngle;
angle = 25 / 100.0 * M_PI * 2;
endAngle = startAngle + angle;
UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path1 addLineToPoint:center];
[[UIColor orangeColor] set];
[path1 fill];
// 第3个扇形
startAngle = endAngle;
angle = 50 / 100.0 * M_PI * 2;
endAngle = startAngle + angle;
UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path2 addLineToPoint:center];
[[UIColor blueColor] set];
[path2 fill];
}
@end
Quartz2D - 画饼图
最新推荐文章于 2018-02-08 12:01:05 发布
本文介绍了一个使用Objective-C编写的自定义UIView子类PieView,用于绘制饼图。该视图能够根据输入的数据数组自动计算并绘制不同比例的扇形区域,并支持随机颜色填充。通过示例展示了如何设置扇形的角度和填充颜色。
377

被折叠的 条评论
为什么被折叠?



