我使用两个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.
有谁知道为什么这些代表永远不会被召唤?
答案 0 :(得分:1)
编辑:使用下面的评论添加正确的解决方案
您正在调用topProgressBar。animation(forKey: "animate")实际返回动画;它没有启动它。您应该致电layer.add(animation, forKey:)而不是