我想实现CABasicAnimation并在动画完成时通知UIViewController。从这个资源:
http://www.informit.com/articles/article.aspx?p=1168314&seqNum=2
我知道我可以将viewcontroller指定为动画的委托,并在viewcontroller中覆盖animationDidStop
方法。但是当我将以下代码行转换为Swift时:
[animation setDelegate:self];
像这样:
animation.delegate = self //没有setDelegate方法
XCode抱怨:
Cannot assign value of type 'SplashScreenViewController' to type 'CAAnimationDelegate?'
我做错了什么?我错过了什么吗?
答案 0 :(得分:8)
您需要确保您的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) {
}
}