效果如下图的圆,中间的内容可自己添加;隐藏中间白色的圆即是一个饼图
调用方法:
GradeView *gradeView = [[GradeView alloc] init];
gradeView.grade = @"0.8";
需要更新需要调用:
gradeView.grade = @"0.9";
[gradeView setNeedsDisplay];
GradeView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface GradeView : UIView
@property (nonatomic, strong) NSString *grade;
@end
NS_ASSUME_NONNULL_END
GradeView.m
#import "GradeView.h"
@implementation GradeView
- (void)setGrade:(NSString *)grade {
_grade = grade;
}
//计算度转弧度
static inline float radians(double degrees) {
return degrees * M_PI / 180;
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGFloat borderWidth = 20;
CGFloat radius = self.bounds.size.height / 2;
CGFloat grade = self.grade ? self.grade.floatValue : 1;
grade = grade > 0 ? grade : 0;
UIColor *bgColor = HEXCOLOR(@"ffffff");
UIColor *tintColor = Color_Global_Light;
UIColor *onTintColor = Color_Global;
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
//填充当区域内的颜色与父视图相同
[bgColor set];
CGContextFillRect(context, rect);
CGContextFillPath(context);
//绘制两个扇形
float angle_start = radians(0.0);
float angle_end = radians(360 * grade);
CGContextMoveToPoint(context, center.x, center.y);
CGContextSetFillColor(context, CGColorGetComponents(onTintColor.CGColor));
CGContextAddArc(context, center.x, center.y, radius, angle_start, angle_end, 0);
CGContextFillPath(context);
angle_start = angle_end;
angle_end = radians(360.0);
CGContextMoveToPoint(context, center.x, center.y);
CGContextSetFillColor(context, CGColorGetComponents(tintColor.CGColor));
CGContextAddArc(context, center.x, center.y, radius, angle_start, angle_end, 0);
CGContextFillPath(context);
//绘制圆颜色与父视图相同
CGFloat w = (self.bounds.size.width)/ 2 - borderWidth;
CGContextAddEllipseInRect(context, CGRectMake(center.x - w, center.y - w, w * 2, w * 2));
[bgColor set];
CGContextFillPath(context);//实心的
CGContextStrokePath(context);//空心的
//调整方向
self.layer.transform = CATransform3DMakeRotation(M_PI * -0.5, 0, 0, 1);
self.layer.shadowRadius = radius;
self.layer.masksToBounds = YES;
}
@end