我使用两个CAShapeLayer's
. 动画工作正常,但CAAnimationDelegate
animationDidStart
和animationDidStop
永远不会被调用。
这是我的代码:
class ProgressBar: UIView, CAAnimationDelegate {
var bottomProgressBar = CAShapeLayer()
var topProgressBar = CAShapeLayer()
let animation = CABasicAnimation(keyPath: "strokeEnd")
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
func configure() {
animation.delegate = self
// setup the two layers
}
func animate(toValue: Double) {
animation.duration = 1.0
animation.fromValue = 0
animation.toValue = toValue
topProgressBar.strokeEnd = CGFloat(toValue)
topProgressBar.animation(forKey: "animate")
}
func animationDidStart(_ anim: CAAnimation) {
print("start")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
print("stop")
}
}
animate
方法被多次调用,值范围从 0 到 1。
有谁知道为什么这些代表从来没有被调用过?
编辑:添加正确的解决方案,并在下面的每个评论中添加链接
您正在调用 topProgressBar。animation(forKey: "animate")实际上返回动画;它不会启动它。你应该调用layer.add(animation, forKey:)代替
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句