gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:16:17 24 4
gpt4 key购买 nike

我想实现 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com