// 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 拼接路径
CGPoint center = CGPointMake(125, 125);
CGFloat radius = 100;
// 第一个扇形
// 起始角度
CGFloat startA = 0;
// 扇形所占角度,注意这里不可以除以100,两个整数相除还是整数,结果为0,怎么有才会有小数,是不是把100改成100.0,自动类型提升为浮点数啊。 角度= 自己所占比例 * 360°
CGFloat angle = 25 / 100.0 * M_PI * 2;
CGFloat endA = startA + angle;
// 扇形路径
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
[path addLineToPoint:center];
[[UIColor redColor] set];
// 将路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 将上下文渲染到视图
CGContextFillPath(ctx);
// 第二个扇形
startA = endA;
angle = 50 / 100.0 * M_PI * 2;
endA = startA + angle;
UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
[path1 addLineToPoint:center];
// 将路径添加到上下文
CGContextAddPath(ctx, path1.CGPath);
[[UIColor greenColor] set];
// 将上下文渲染到视图
CGContextFillPath(ctx);
// 第三个扇形
startA = endA;
angle = 25 / 100.0 * M_PI * 2;
endA = startA + angle;
UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
[path2 addLineToPoint:center];
// 将路径添加到上下文
CGContextAddPath(ctx, path2.CGPath);
[[UIColor blueColor] set];
// 将上下文渲染到视图
CGContextFillPath(ctx);
iOS画饼状图
猜你喜欢
转载自blog.csdn.net/BianHuanShiZhe/article/details/104996054
今日推荐
周排行