iPhone 利用CG API画一个饼图(Pie chart) 可以用在很多地方 画弧线

核心函数是:CGContextAddArc(CGContextRefc,CGFloatx,CGFloaty,CGFloatradius,CGFloatstartAngle,CGFloatendAngle,intclockwise)

  • CGContextRef: 图形上下文
  • x,y: 开始画的坐标
  • radius: 半径
  • startAngle, endAngle: 开始的弧度,结束的弧度
  • clockwise: 画的方向(顺时针,逆时针)

有了这个函数可以画出任意扇形,所以饼图也不再话下.
Java代码 收藏代码
  1. #definePI3.14159265358979323846
  2. #defineradius100
  3. staticinlinefloatradians(doubledegrees){
  4. returndegrees*PI/180;
  5. }
  6. staticinlinevoiddrawArc(CGContextRefctx,CGPointpoint,floatangle_start,floatangle_end,UIColor*color){
  7. CGContextMoveToPoint(ctx,point.x,point.y);
  8. CGContextSetFillColor(ctx,CGColorGetComponents([colorCGColor]));
  9. CGContextAddArc(ctx,point.x,point.y,radius,angle_start,angle_end,0);
  10. //CGContextClosePath(ctx);
  11. CGContextFillPath(ctx);
  12. }
  13. -(void)drawRect:(CGRect)rect{
  14. CGContextRefctx=UIGraphicsGetCurrentContext();
  15. CGContextClearRect(ctx,rect);
  16. floatangle_start=radians(0.0);
  17. floatangle_end=radians(121.0);
  18. drawArc(ctx,self.center,angle_start,angle_end,[UIColoryellowColor]);
  19. angle_start=angle_end;
  20. angle_end=radians(228.0);
  21. drawArc(ctx,self.center,angle_start,angle_end,[UIColorgreenColor]);
  22. angle_start=angle_end;
  23. angle_end=radians(260);
  24. drawArc(ctx,self.center,angle_start,angle_end,[UIColororangeColor]);
  25. angle_start=angle_end;
  26. angle_end=radians(360);
  27. drawArc(ctx,self.center,angle_start,angle_end,[UIColorpurpleColor]);
  28. }

你可能感兴趣的:(iPhone)