核心函数是:CGContextAddArc(CGContextRefc,CGFloatx,CGFloaty,CGFloatradius,CGFloatstartAngle,CGFloatendAngle,intclockwise)
- CGContextRef: 图形上下文
- x,y: 开始画的坐标
- radius: 半径
- startAngle, endAngle: 开始的弧度,结束的弧度
- clockwise: 画的方向(顺时针,逆时针)
有了这个函数可以画出任意扇形,所以饼图也不再话下.
- #definePI3.14159265358979323846
- #defineradius100
- staticinlinefloatradians(doubledegrees){
- returndegrees*PI/180;
- }
- staticinlinevoiddrawArc(CGContextRefctx,CGPointpoint,floatangle_start,floatangle_end,UIColor*color){
- CGContextMoveToPoint(ctx,point.x,point.y);
- CGContextSetFillColor(ctx,CGColorGetComponents([colorCGColor]));
- CGContextAddArc(ctx,point.x,point.y,radius,angle_start,angle_end,0);
- //CGContextClosePath(ctx);
- CGContextFillPath(ctx);
- }
- -(void)drawRect:(CGRect)rect{
- CGContextRefctx=UIGraphicsGetCurrentContext();
- CGContextClearRect(ctx,rect);
- floatangle_start=radians(0.0);
- floatangle_end=radians(121.0);
- drawArc(ctx,self.center,angle_start,angle_end,[UIColoryellowColor]);
- angle_start=angle_end;
- angle_end=radians(228.0);
- drawArc(ctx,self.center,angle_start,angle_end,[UIColorgreenColor]);
- angle_start=angle_end;
- angle_end=radians(260);
- drawArc(ctx,self.center,angle_start,angle_end,[UIColororangeColor]);
- angle_start=angle_end;
- angle_end=radians(360);
- drawArc(ctx,self.center,angle_start,angle_end,[UIColorpurpleColor]);
- }