Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation Delegate not converting to Swift 3.0

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?

like image 892
user594883 Avatar asked Jan 05 '23 14:01

user594883


1 Answers

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) {

    }
}
like image 159
Pierce Avatar answered Jan 14 '23 18:01

Pierce