天堂向右,我依然向左

天下之大,虽离家千里,何处不可往!何事不可为!
生活之路,纵坎坷曲折,当奋斗不息,则精彩纷呈!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

核心函数是:CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, intclockwise)

 

  • CGContextRef: 图形上下文
  • x,y: 开始画的坐标
  • radius: 半径
  • startAngle, endAngle: 开始的弧度,结束的弧度
  • clockwise: 画的方向(顺时针,逆时针)
 
有了这个函数可以画出任意扇形,所以饼图也不再话下. 
Java代码 
  1. #define PI 3.14159265358979323846  
  2. #define radius 100  
  3.   
  4. static inline float radians(double degrees) {   
  5.     return degrees * PI / 180;   
  6. }  
  7.   
  8. static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor* color) {  
  9.     CGContextMoveToPoint(ctx, point.x, point.y);  
  10.     CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));      
  11.     CGContextAddArc(ctx, point.x, point.y, radius,  angle_start, angle_end, 0);  
  12.     //CGContextClosePath(ctx);   
  13.     CGContextFillPath(ctx);   
  14. }  
  15.   
  16. - (void)drawRect:(CGRect)rect {  
  17.       
  18.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  19.     CGContextClearRect(ctx, rect);  
  20.       
  21.       
  22.     float angle_start = radians(0.0);  
  23.     float angle_end = radians(121.0);     
  24.     drawArc(ctx, self.center, angle_start, angle_end, [UIColor yellowColor]);  
  25.       
  26.       
  27.     angle_start = angle_end;  
  28.     angle_end = radians(228.0);   
  29.     drawArc(ctx, self.center, angle_start, angle_end, [UIColor greenColor]);  
  30.   
  31.       
  32.     angle_start = angle_end;  
  33.     angle_end = radians(260);  
  34.     drawArc(ctx, self.center, angle_start, angle_end, [UIColor orangeColor]);  
  35.       
  36.       
  37.     angle_start = angle_end;  
  38.     angle_end = radians(360);  
  39.     drawArc(ctx, self.center, angle_start, angle_end, [UIColor purpleColor]);  
  40. }  
 


posted on 2010-08-24 15:42  老舟  阅读(381)  评论(0编辑  收藏  举报