啥也不说了,直接上代码
-(void)circlePath{
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, self.view.center.y) radius:100 startAngle:0 endAngle:(_progress/100)*M_PI*2 clockwise:YES];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = self.view.bounds;
shapeLayer.path = circlePath.CGPath;
shapeLayer.opacity = 1.0f;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.lineWidth = 5.0f;
shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
[self.view.layer addSublayer:shapeLayer];
NSMutableArray *colors = [NSMutableArray array];
for (NSInteger i = 0; i <= 360; i += 5) {
UIColor *color = [UIColor colorWithHue:1.0*i/360.0 saturation:1.0 brightness:1.0 alpha:1.0];
[colors addObject:(id)color.CGColor];
}
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.view.bounds;
gradientLayer.colors = colors;
gradientLayer.mask = shapeLayer;
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 0);
[self.view.layer addSublayer:gradientLayer];
_textLayer = [CATextLayer layer];
float width = [UIScreen mainScreen].bounds.size.width;
float height = [UIScreen mainScreen].bounds.size.height;
_textLayer.frame = CGRectMake(width/2-30, height/2, 100, 100);
_textLayer.foregroundColor = [UIColor redColor].CGColor;
_textLayer.fontSize = 14.0f;
[self.view.layer addSublayer:_textLayer];
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
basicAnimation.duration = 5.0f;
basicAnimation.fromValue = @0;
basicAnimation.toValue = @1;
[shapeLayer addAnimation:basicAnimation forKey:@"key"];
}
显示进度百分比
self.progress = 75;
[self circlePath];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01*3 repeats:YES block:^(NSTimer * _Nonnull timer) {
if (_count < _progress) {
_count ++;
_textLayer.string = [NSString stringWithFormat:@"%.f%%",_count];
}
}];