2017-02-05 173 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) { 

    } 
}