// // CircularProgressView.m // HD wallpaper // #import "CircularProgressView.h" @implementation CircularProgressView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _progress = 0.0; _progressColor = [UIColor blueColor]; _trackColor = [UIColor lightGrayColor]; self.backgroundColor = [UIColor clearColor]; } return self; } - (void)setProgress:(CGFloat)progress { _progress = progress; [self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGFloat lineWidth = 10.0; CGRect circleRect = CGRectInset(self.bounds, lineWidth / 2.0, lineWidth / 2.0); // 画背景圆环 CGContextSetStrokeColorWithColor(context, self.trackColor.CGColor); CGContextSetLineWidth(context, lineWidth); CGContextStrokeEllipseInRect(context, circleRect); // 画进度条 CGFloat startAngle = -M_PI_2; CGFloat endAngle = startAngle + self.progress * 2.0 * M_PI; CGContextSetStrokeColorWithColor(context, self.progressColor.CGColor); CGContextSetLineWidth(context, lineWidth); CGContextAddArc(context, CGRectGetMidX(circleRect), CGRectGetMidY(circleRect), CGRectGetWidth(circleRect) / 2.0, startAngle, endAngle, 0); CGContextStrokePath(context); } //#import "CircularProgressView.h" // //// 创建并添加CircularProgressView //CircularProgressView *progressView = [[CircularProgressView alloc] initWithFrame:CGRectMake(100-1,100-1,200-1,200-1)]; //progressView.progressColor = [UIColor redColor]; //progressView.trackColor = [UIColor grayColor]; //[self.view addSubview:progressView]; // //// 设置进度 //progressView.progress = 0.75; // 75% 进度 @end