动画代表不转换为Swift 3.0

use*_*883 4 delegates core-animation caanimation ios swift

我想实现CABasicAnimation并在动画完成时通知UIViewController.从这个资源:

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

我知道我可以将viewcontroller指定为viewcontroller中动画和覆盖animationDidStop方法的委托.但是当我将以下代码行转换为Swift时:

[animation setDelegate:self];
Run Code Online (Sandbox Code Playgroud)

像这样:

animation.delegate = self //没有setDelegate方法

XCode抱怨:

Cannot assign value of type 'SplashScreenViewController' to type 'CAAnimationDelegate?'
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我错过了什么吗?

Pie*_*rce 9

您需要确保viewController符合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

}
Run Code Online (Sandbox Code Playgroud)

您还可以使用扩展名来符合代理人:

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

    }

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

    }
}
Run Code Online (Sandbox Code Playgroud)