iOS之画饼图
来源:互联网 发布:java线程概念 编辑:程序博客网 时间:2024/05/16 12:22
iOS之画饼图
1.效果图如下
2.画饼图实现步骤如下:
1.在main.storyboard中拖入一个UIView控件,并设置其Custom Class 为HMPieView。如下图
2.新建HMPieView类
//// HMPieView.h// 03-饼图//#import <UIKit/UIKit.h>@interface HMPieView : UIView@end
//// HMPieView.m// 03-饼图//#import "HMPieView.h"#import "UIColor+Random.h"@implementation HMPieView- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code } return self;}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{ // Drawing code NSArray *data = @[@25,@25,@50]; // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径 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 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES]; [path addLineToPoint:center]; [[UIColor randomColor] set]; // 把路径添加上下文 CGContextAddPath(ctx, path.CGPath); // 渲染 CGContextFillPath(ctx); }}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ CGFloat a = arc4random_uniform(6); //CGFloat a = arc4random()%6; NSLog(@"随机数--%f",a); [self setNeedsDisplay];}- (void)drawPie{ // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径 CGPoint center = CGPointMake(125, 125); CGFloat radius = 120; CGFloat startA = 0; CGFloat angle = 0; CGFloat endA = 0; // 第一个扇形 angle = 25 / 100.0 * M_PI * 2; endA = startA + angle; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES]; [path addLineToPoint:center]; // 添加到上下文 CGContextAddPath(ctx, path.CGPath); [[UIColor redColor] set]; // 渲染 CGContextFillPath(ctx); // 第二个扇形 startA = endA; angle = 25 / 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 = 50 / 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); }@end
- 3.新建UIColor+Random类
//// UIColor+Random.h// 03-饼图//#import <UIKit/UIKit.h>@interface UIColor (Random)+ (UIColor *)randomColor;@end
//// UIColor+Random.m// 03-饼图//#import "UIColor+Random.h"@implementation UIColor (Random)+ (UIColor *)randomColor{ /* 颜色有两种表现形式 RGB RGBA RGB 24 R,G,B每个颜色通道8位 8的二进制 255 R,G,B每个颜色取值 0 ~255 120 / 255.0 */ 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];}@end
1 0
- iOS之画饼图
- 三 iOS之 画饼图
- iOS 画饼状图
- iOS 画饼图
- iOS 数组~画饼图-添加数据优化
- 画饼图
- 画饼图
- 画饼图
- 画饼图
- IOS开发-画饼图画柱状图
- asp.net画饼图
- Pie画饼图
- Flot画饼图
- asp.net画饼图
- 利用JqPlot画饼图
- 使用QPainter 画饼图
- 利用applet画饼图
- zedGraph画饼图
- 提交application/x-www-form-urlencoded类型数据
- iOS开发 之iOS沙盒(sandbox)机制和文件操作(一)
- 打开/保存文件对话框:CFileDialog
- ubuntu安装
- 从时间序列到复杂网络:可见图算法
- iOS之画饼图
- jasig CAS实现单点登录(数据库认证)
- POJ 1934 【LCS加强版。。】
- 盒子模型
- Servlet 输出中文乱码问题
- 你好,面试官
- net/sourceforge/cobertura/coveragedata/TouchColl
- JSP内置对象
- mysql 存在该记录则更新,不存在则插入记录的sql