Reputation: 20068
I made a simple progress line using two CAShapeLayer's
. Animation works correctly but the CAAnimationDelegate
animationDidStart
and animationDidStop
are never called.
Here is my code:
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
method is called multiple times with values ranging from 0 to 1.
Does anyone know why these delegates are never called ?
Upvotes: 0
Views: 1114
Reputation: 16327
EDIT: add correct solution with links per comment below
You are calling topProgressBar.animation(forKey: "animate") which actually returns the animation; it doesn't start it. You should call layer.add(animation, forKey:) instead
Upvotes: 1