Ios CAAnimationDelegate代理从不被调用
我用两个Ios CAAnimationDelegate代理从不被调用,ios,swift,cabasicanimation,Ios,Swift,Cabasicanimation,我用两个CAShapeLayer的制作了一个简单的进度线。动画工作正常,但从不调用CAAnimationDelegateanimationDidStart和animationDidStop 这是我的密码: class ProgressBar: UIView, CAAnimationDelegate { var bottomProgressBar = CAShapeLayer() var topProgressBar = CAShapeLayer() let animat
CAShapeLayer的
制作了一个简单的进度线。动画工作正常,但从不调用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。它实际上返回动画;它不会启动它。您应该改为调用是否将动画添加到
CALayer
?是topProgressBar.animation(forKey:“animate”)
应该是topProgressBar.add(animation,forKey:“animate”)
?我从不添加它。我应该吗?因为对我来说,这个代码是有效的。但是现在我想看看动画什么时候开始和结束,这部分不起作用。显然,我必须将动画添加到CAShapeLayer,然后它被调用。我正在查看我的代码,你必须调用layer。添加(动画,forKey:)而不是调用topProgressBar.animation(forKey:“animate”),它实际上返回动画;它不会启动你能用这个更新答案吗?我会接受的。谢谢