三 iOS之 画饼图
来源:互联网 发布:qrcode.js 解析二维码 编辑:程序博客网 时间:2024/06/05 18:07
这次绘制一个可随机变换的饼图
新建一个UIView — PieView
在 PieView.m 写入
//做一个随机数组-(NSArray*)arrRandom{ //数组中所有数的总和为100 int totoal = 100; NSMutableArray * arrM = [NSMutableArray array]; int temp = 0; //设置循环的随机次数不超过10, +1是因为随机数可能为0,如果是0没有意义 for (int i =0; i < arc4random_uniform(10) + 1; i++) { //随机分配的数 temp = arc4random_uniform(totoal) + 1; //将随机数加到数组里 [arrM addObject:@(temp)]; // 解决方式:当随机出来的数等于总数直接退出循环。 if (temp == totoal) { break; } //每循环一次,就减掉一个随机数 totoal -= temp; } //退出循环之后,如果总数中还有剩余值就加上最后的值 if (totoal) { [arrM addObject:@(totoal)]; } return arrM;}- (void)drawRect:(CGRect)rect {// NSArray * arr = @[@25,@25,@50]; NSArray * arr = [self arrRandom]; //半径的长度 rect:控件本身的bounds CGFloat radius = rect.size.width * 0.5; //中心点 CGPoint center = CGPointMake(radius, radius); CGFloat startA = 0; CGFloat endA = 0; CGFloat angle = 0; for (int i = 0; i < arr.count; i++) { startA = endA; //所画出来的扇形的角度 M_PI * 2 : 360˚ angle = [arr[i] integerValue] / 100.0 * M_PI * 2; endA = startA + angle; UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES]; [path addLineToPoint:center]; //设置随机颜色 [[self colorRandom] set]; [path fill]; }}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self setNeedsDisplay];}//做一个随机颜色-(UIColor*)colorRandom{ 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];}
github demo: PieDemo
阅读全文
1 0
- 三 iOS之 画饼图
- iOS之画饼图
- iOS 画饼状图
- iOS 画饼图
- iOS 数组~画饼图-添加数据优化
- 画饼图
- 画饼图
- 画饼图
- 画饼图
- IOS开发-画饼图画柱状图
- asp.net画饼图
- Pie画饼图
- Flot画饼图
- asp.net画饼图
- 利用JqPlot画饼图
- 使用QPainter 画饼图
- 利用applet画饼图
- zedGraph画饼图
- 允许mysql同时插入多条sql语句
- Fragment接口回调(左右分屏显示)
- 字符串哈希的小技巧
- Struts2和MVC的关系
- Caffe学习之caffe具体运行流程分析
- 三 iOS之 画饼图
- 用java打印出三位数中的“水仙花数”
- 动脉硬怎么办
- 下载安装 Apache(Windows 64位)
- SVPWM算法原理及详解
- Struts2系列之配置文件中常用的标签
- swap的三种写法
- 【C语言】缓存区问题
- python code segment daily collect