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