2017-02-05 125 views
0

我想实现CABasicAnimation并在动画完成时通知UIViewController。从这个资源:动画委托不转换为Swift 3.0

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

我明白,我可以指定视图 - 控制作为动画的委托,并重写的ViewController内animationDidStop方法。然而,当我转换的代码以下行斯威夫特:

[animation setDelegate:self]; 

像这样:

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

的XCode抱怨:

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

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

回答

4

你需要确保你的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 

} 

您还可以通过使用扩展的符合委托:

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

    } 

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

    } 
}