首页 > 解决方案 > 根据值(360)或百分比(100%)创建彩色圆圈

问题描述

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

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];

标签: iosobjective-cxcodecashapelayercaanimation

解决方案


这意味着您toValue应该匹配您的百分比。

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

有关在完成后保留最终动画值的信息,请参阅此答案。

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

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

推荐阅读