永远不会调用 CAAnimationDelegate 委托
CAAnimationDelegate delegates are never called
我用两个 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") which actually returns the animation; it doesn't start it. You should call layer.add(animation, forKey:) 而不是
我用两个 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") which actually returns the animation; it doesn't start it. You should call layer.add(animation, forKey:) 而不是