我想创建一个具有圆弧形式的进度栏。进度条的颜色必须根据值更改。将渐变颜色应用于使用UIBezierPath创建的圆弧
我使用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
很高兴它帮助:) – djshiow
这看起来不会沿着路径绘制**,而是,它似乎会绘制一个水平渐变,这是通过路径显示。那有意义吗?你如何“弯曲”一个水平渐变以遵循整个圆圈? – tracicot