我想按百分比显示我的类别的饼图。
如何创建类别百分比的饼图?
您必须实现一个覆盖该drawRect:
方法的类并自己绘制饼图。您将使用UIBezierPath类,特别是查看addArcWithCenter:radius:startAngle:endAngle:itarian:方法来绘制圆的一部分。
这可能有助于警告如果它看起来像其他人的代码,那是因为我制定了如何在网站上执行此操作,以及我在启动 iPhone 后不久就执行此操作的附加警告。欢迎那些想告诉我哪些位效率低下或错误的人,我还在学习。
static inline float radians(double degrees) { return degrees * M_PI / 180; }
// making a simple pac man shape
- (UIImage*)getImage {
UIImage* image;
if(self.completion == 100.0f) {
image = [UIImage imageNamed:@"completedTaskIcon.png"];
} else {
UIGraphicsBeginImageContext(CGSizeMake(SIDELENGTH, SIDELENGTH));
CGContextRef context = UIGraphicsGetCurrentContext();
// the image should have a clear background
[[UIColor clearColor] set];
CGRect myRect = CGRectMake(0.0f, 0.0f, SIDELENGTH, SIDELENGTH);
UIRectFill(myRect);
// color was hopefully set before this method called
[self.color set];
// centre point is required
CGFloat midx = SIDELENGTH/2;
CGFloat midy = SIDELENGTH/2;
// radius of the arc
CGFloat radius = (SIDELENGTH/2) * 0.60f;
// pie background
CGContextSetFillColor(context, CGColorGetComponents([[UIColor orangeColor] CGColor]));
CGContextBeginPath(context);
CGContextMoveToPoint(context, midx + radius, midy);
CGContextAddArc(context, midx, midy, radius, radians(0), radians(360), 0);
CGContextFillPath(context);
// pie segment
CGContextSetFillColor(context, CGColorGetComponents([[UIColor blueColor] CGColor]));
CGContextBeginPath(context);
CGContextMoveToPoint(context, midx, midy);
CGContextAddLineToPoint(context, midx + radius, midy);
CGContextAddArc(context, midx, midy, radius, radians(0), radians(360 * (self.completion / 100.0f)), 0);
CGContextFillPath(context);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return image;
}