【问题标题】:Animation Delegate not converting to Swift 3.0动画委托未转换为 Swift 3.0
【发布时间】:2017-02-05 19:14:13
【问题描述】:

我想实现 CABasicAnimation 并在动画完成时通知 UIViewController。来自此资源:

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

我知道我可以将 viewcontroller 指定为动画的委托,并在 viewcontroller 中覆盖 animationDidStopmethod。但是,当我将以下代码行转换为 Swift 时:

[animation setDelegate:self];

像这样:

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

XCode 抱怨:

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

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

【问题讨论】:

    标签: ios swift delegates core-animation caanimation


    【解决方案1】:

    您需要确保您的 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) {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 2016-07-26
      相关资源
      最近更新 更多