根据值(360)或百分比(100%)创建彩色圆圈

Create coloured circle based on value(360) or percentage(100%)

此代码创建一个完整的圆,但我想创建一个基于值 360 或百分比 100% 的圆。如果百分比是 56%,我想要 56% 的圆。像这样基于percentage/value,我想要圈子

CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(29, 29) radius:27 startAngle:-M_PI_2 endAngle:2 * M_PI - M_PI_2 clockwise:YES].CGPath;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor greenColor].CGColor;
circle.lineWidth = 4;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.duration = 10;
animation.removedOnCompletion = NO;
animation.fromValue = @(0);
animation.toValue = @(1);
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[circle addAnimation:animation forKey:@"drawCircleAnimation"];

[_colouredCircle.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
[_colouredCircle.layer addSublayer:circle];

这意味着您的 toValue 应该与您的百分比相符。

在您的情况下,animation.toValue = @(0.56); 会在圆的 56%(0...1 范围)处结束笔划。

See this answer 了解有关在完成后保留最终动画值的信息。

TLDR:您还必须更新模型层。

circle.strokeStart = 0;
circle.strokeEnd   = 0.56; // Your target value
...
// Your animation

查看this 圆形进度条并根据需要在特定位置设置颜色。