ios - 动画委托(delegate)未转换为 Swift 3.0

标签 ios swift delegates core-animation caanimation

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

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

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

[animation setDelegate:self];

像这样:

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

XCode 提示:

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

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

最佳答案

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

}

您还可以使用扩展来符合委托(delegate):

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

    }

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

    }
}

关于ios - 动画委托(delegate)未转换为 Swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42056122/

相关文章:

ios - Xcode iOS 模拟器离开 Black Space

ios - 更改状态栏样式中间滚动

ios - 如何为 URLSessionDataTask 分配委托(delegate)?

ios - 在父 View Controller 中管理几个 UIViewController

iOS:从 NSMutableArray 中获取 3 个不同的随机元素

javascript - 如何从我的 html 文件移动到另一个 html 文件以创建 iOS 混合应用程序?

ios - 当单元格包含特定项目时,如何找到自定义单元格的索引路径?

ios - 准备在 UITabbarController 中不调用 segue

objective-c - ios - 在子类化时从 UIPickerView 传递值

javascript - 单击一个选择的 html 元素