iPhone 利用CG API画一个饼图(Pie chart) 百分比圆 以及 响应扇形点击事件
2011-10-12 10:10
337 查看
核心函数是:CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise) * CGContextRef: 图形上下文 * x,y: 开始画的坐标 * radius: 半径 * startAngle, endAngle: 开始的弧度,结束的弧度 * clockwise: 画的方向(顺时针,逆时针)
有了这个函数可以画出任意扇形,所以饼图也不再话下.
#define PI 3.14159265358979323846 #define radius 100 static inline float radians(double degrees) { return degrees * PI / 180; } static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor* color) { CGContextMoveToPoint(ctx, point.x, point.y); CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor])); CGContextAddArc(ctx, point.x, point.y, radius, angle_start, angle_end, 0); //CGContextClosePath(ctx); CGContextFillPath(ctx); } - (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextClearRect(ctx, rect); float angle_start = radians(0.0); float angle_end = radians(121.0); drawArc(ctx, self.center, angle_start, angle_end, [UIColor yellowColor]); angle_start = angle_end; angle_end = radians(228.0); drawArc(ctx, self.center, angle_start, angle_end, [UIColor greenColor]); angle_start = angle_end; angle_end = radians(260); drawArc(ctx, self.center, angle_start, angle_end, [UIColor orangeColor]); angle_start = angle_end; angle_end = radians(360); drawArc(ctx, self.center, angle_start, angle_end, [UIColor purpleColor]); }
效果:
![](http://hi.csdn.net/attachment/201110/12/0_1318385575b32e.gif)
想想,我用这个函数,画两个同圆心的扇形
大的用自己想要的颜色,小的用背景色
这样就实现了画环形了,多谢大神!
我来补充点:可以使用UIBezierPath的处理扇形区域的点击事件
原文地址:http://www.devdiv.com/forum.php?mod=viewthread&tid=38136&fromuid=36506
相关文章推荐
- iPhone 利用CG API画一个饼图(Pie chart)
- iPhone 利用CG API画一个饼图(Pie chart) 可以用在很多地方 画弧线
- iPhone 利用CG API画一个饼图(Pie chart)
- iphone 让一个UIImageView响应点击事件
- 利用RecycleView实现类似ListView的Item点击,长按等操作事件以及点击后每一项在添加一个列表
- 如何为一个按钮(View)设置任意多次点击事件以及可以同时响应多个多次点击事件
- 怎么让一个UIImageView响应点击事件呢
- Unity 3D物体的点击事件响应以及NGUI坐标和世界坐标的互相转换
- 利用JS 事件 与 Cnavas绘图 以及 H5 缓存写的一个手势解锁(三)
- c# 给button添加不规则的图片以及用pictureBox替代button响应点击事件
- listview adapter 控件注册点击事件,点击一个item后有其他item的这个控件响应了事件
- checkbox扩大点击范围以及一个监听器处理多个点击事件
- 一个form表单中两个按钮点击事件以及系统提示用户
- Android 点击一个圆上的不同的扇形产生不同的事件
- 让一个UIImageView响应点击事件
- 让一个UIImageView响应点击事件
- Winform不同窗口的Button响应同一个点击事件
- Android深入探究笔记之二 -- 打开一个新的 Activity 并传递参数与如何响应控件的点击事件
- c# 给button添加不规则的图片以及用pictureBox替代button响应点击事件的方法
- 怎么让一个UIImageView响应点击事件呢