分类: BSD
2011-01-14 16:48:41
最近的项目要在iPhone应用中画饼图统计报表,其中画扇形部分的的确确把我难住了!经过艰苦的资料奋斗,终于把问题解决了,但是我必须说一句,iPhone里,在画弧线时的计算单位不是角度,而是弧度!
画扇形基本代码如下:
-(void)paintpie:(CGContextRef)ctx
start:(double)pieStart
capacity:(double)pieCapacity
pointx:(double)x
pointy:(double)y
piecolor:(UIColor *)color{
//起始角度,0-360
double snapshot_start = pieStart;
//结束角度
double snapshot_finish = pieStart+pieCapacity;
//设置扇形填充色
CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));
//设置圆心
CGContextMoveToPoint(ctx, x, y);
//以90为半径围绕圆心画指定角度扇形,0表示逆时针
CGContextAddArc(ctx, x, y, 90, radians(snapshot_start), radians(snapshot_finish), 0);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
//把角度转换为弧度的计算公式
#define PI 3.14159265358979323846
static inline float radians(double degrees) { return degrees * PI / 180; }