BasicAnimation,Swift

问题描述 投票:0回答:1

我这样定义了一个圆:

let progressLayer = CAShapeLayer()
let ring = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2 , endAngle: 2 * CGFloat.pi , clockwise: true)

progressLayer.path = ring.cgPath
progressLayer.strokeColor = UIColor.green.cgColor
progressLayer.fillColor = UIColor.clear.cgColor
view.layer.addSublayer(progressLayer)

然后我定义了一个动画,该动画用这样的颜色和持续时间填充了圆的边界:

let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
basicAnimation.duration = 60 //Seconds !
basicAnimation.fillMode = .forwards
progressLayer.add(basicAnimation, forKey: "blablabla")

发生的事情是动画以12秒的延迟到达了圆的尽头! (圆圈已满,但还剩12秒)为什么会这样呢?是三角学问题(startAngle endAngle)吗?我听不懂...

ios swift xcode geometry cabasicanimation
1个回答
0
投票

贝塞尔曲线路径存在问题,您正在向圆添加额外的四分之一,

因此,圆在(60/5)* 4 = 48秒]处填充,而其他12秒在已经填充的四分之一上绘制。

将起始角度设置为0将解决此问题。

let ring = UIBezierPath(arcCenter: center, radius: 100, startAngle: CGFloat(0) , endAngle: 2 * CGFloat.pi , clockwise: true)
© www.soinside.com 2019 - 2024. All rights reserved.