iPhone 利用CG API画一个饼图(Pie chart) 百分比圆 以及 响应扇形点击事件

核心函数是: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]);
}

效果:



想想,我用这个函数,画两个同圆心的扇形
大的用自己想要的颜色,小的用背景色
这样就实现了画环形了,多谢大神!


我来补充点:可以使用UIBezierPath的处理扇形区域的点击事件


原文地址:http://www.devdiv.com/forum.php?mod=viewthread&tid=38136&fromuid=36506



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值