Reputation: 5451
I'm not sure what's wrong, I made sure to set the delegate to self but it's still not being called. Here is the code:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//animate form
let flyRight = CABasicAnimation(keyPath: "position.x")
flyRight.fromValue = -view.bounds.size.width/2
flyRight.toValue = view.bounds.size.width/2
flyRight.duration = 0.5
heading.layer.add(flyRight, forKey: nil)
flyRight.isRemovedOnCompletion = false
flyRight.fillMode = kCAFillModeForwards
flyRight.delegate = self as? CAAnimationDelegate
flyRight.setValue("form", forKey: "name")
flyRight.setValue(username.layer, forKey: "layer")
}
Here is the animationDidStop
:
func animationDidStop(_ anim: CAAnimation!, finished flag: Bool) {
let nameValue = anim.value(forKey: "name") as? String
if let name = nameValue {
if name == "form" {
let layer: CALayer = anim.value(forKey: "layer")! as! CALayer
layer.position.x = view.bounds.width/2
anim.setValue(nil, forKey: "layer")
}
}
}
Upvotes: 1
Views: 735
Reputation: 1814
In your class declaration you are missing conforming to protocol
class MyViewController: UIViewController, CAAnimationDelegate {
// ...
}
Upvotes: 1