青山长在 發表於 2021-2-20 14:36:00

IOS开发之——绘制饼状图

<p>文章搬运来源:https://blog.csdn.net/Calvin_zhou/article/details/112168274<br>
作者:PGzxc</p>
<p><strong>对iOS开发感兴趣,可以看一下作者的iOS交流群:812157648,大家可以在里面吹水、交流相关方面的知识,群里还有我整理的有关于面试的一些资料,欢迎大家加群,大家一起开车</strong></p>
<h2 id="一-概述">一 概述</h2>
<ul>
<li>按照1:1:2的份额划分圆形</li>
<li>开始角度为0度,旋转角度为份额2π(360度),结束角度为开始角度+旋转角度</li>
<li>给每分饼状图设置不同的颜色</li>
<li>点击图形,饼状图切换颜色</li>
</ul>
<h2 id="二-饼状图划分">二 饼状图划分</h2>
<p><img src="https://upload-images.jianshu.io/upload_images/25330009-dbae2800a1e783eb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<pre><code>startA=0;
angle=25/100.0*M_PI*2;
endA=startA=angle;

</code></pre>
<h2 id="三-功能开发">三 功能开发</h2>
<h3 id="31-代码">3.1 代码</h3>
<h4 id="pieview">pieView</h4>
<pre><code>- (void)drawRect:(CGRect)rect {
    // Drawing code
    NSArray *data=@[@25,@25,@50];
    //1.获取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    CGPoint center=CGPointMake(125, 125);
    CGFloat radius=120;
    CGFloat startA=0;
    CGFloat angle=0;
    CGFloat endA=0;

    for (NSNumber *number in data) {
      //2.拼接路径
      startA=endA;
      angle=number.intValue/100.0*M_PI*2;
      endA=startA+angle;

      UIBezierPath *path=;
      ;
      [set];
      //3.把路径添加到上下文
      CGContextAddPath(ctx, path.CGPath);
      //4.渲染
      CGContextFillPath(ctx);
    }
}
- (void)touchesBegan:(NSSet&lt;UITouch *&gt; *)touches withEvent:(UIEvent *)event
{
    ;
}

</code></pre>
<h4 id="uicolorrandomcolor随机颜色工具">UIColor+RandomColor(随机颜色工具)</h4>
<pre><code>#import "UIColor+RandomColor.h"

@implementation UIColor (RandomColor)
+(UIColor *)randomColor
{
    CGFloat r=arc4random_uniform(256)/255.0;
    CGFloat g=arc4random_uniform(256)/255.0;
    CGFloat b=arc4random_uniform(256)/255.0;
    return;

}
@end

</code></pre>
<h3 id="32-效果图">3.2 效果图</h3>
<p><img src="https://upload-images.jianshu.io/upload_images/25330009-6c04d094098de200.gif?imageMogr2/auto-orient/strip"></p><br><br>
来源:https://www.cnblogs.com/fadaijun/p/14420797.html
頁: [1]
查看完整版本: IOS开发之——绘制饼状图