我想创建一个以弧形的形式呈现的进度条。进度条的颜色需要根据数值变化。
我使用 UIBezierPath bezierPathWithArcCenter
创建了一个弧形。以下是我使用的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
int radius = 100;
CAShapeLayer *arc = [CAShapeLayer layer];
arc.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 50) radius:radius startAngle:60.0 endAngle:0.0 clockwise:YES].CGPath;
arc.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
arc.fillColor = [UIColor clearColor].CGColor;
arc.strokeColor = [UIColor purpleColor].CGColor;
arc.lineWidth = 15;
[self.view.layer addSublayer:arc];
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 5.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..
drawAnimation.removedOnCompletion = NO; // Remain stroked after the animation..
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:10.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[arc addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
}
结果看起来像这样:
我的问题是:如果值<= 50%,如何将渐变应用于颜色?我还创建了一个UIButton,它生成随机的CGFloat数字,以便将其与进度条挂钩。有人有什么想法如何解决这个问题吗?
渐变效果会像这样:
希望能得到任何帮助!
非常感谢。
Granit
drawAnimation.toValue = [NSNumber numberWithFloat:10.0f];
这句话是什么意思?为什么是10.0f? - Van Du Tran