4

I want to implement CABasicAnimation and have the UIViewController notified when the animation is completed. From this resource:

http://www.informit.com/articles/article.aspx?p=1168314&seqNum=2

I understood that I can specify the viewcontroller as a delegate for the animation and override animationDidStopmethod within the viewcontroller. However when I convert the following line of code into Swift:

[animation setDelegate:self];

like so:

animation.delegate = self //there are no setDelegate method

XCode complains:

Cannot assign value of type 'SplashScreenViewController' to type 'CAAnimationDelegate?'

What am I doing wrong? Am I missing something?

user594883
  • 1,329
  • 2
  • 17
  • 36

1 Answers1

9

You need to make sure your viewController conforms to the CAAnimationDelegate.

class SplashScreenViewController: UIViewController, CAAnimationDelegate {

    // your code, viewDidLoad and what not

    override func viewDidLoad() {
        super.viewDidLoad()

        let animation = CABasicAnimation()
        animation.delegate = self
        // setup your animation

    }

    // MARK: - CAAnimation Delegate Methods
    func animationDidStart(_ anim: CAAnimation) {

    }

    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {

    }

    // Add any other CAAnimationDelegate Methods you want

}

You can also conform to the delegate by use of an extension:

extension SplasScreenViewController: CAAnimationDelegate {
    func animationDidStart(_ anim: CAAnimation) {

    }

    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {

    }
}
Pierce
  • 3,148
  • 16
  • 38