我正在开发一个包含 WYPopoverController 的应用程序,它在应该的地方工作得很好。我想在两个控制器之间添加自定义动画,而不是内置推送/弹出。
在包含 FROM 和 TO 控制器的导航控制器中,我不做任何与 WYPopoverController 相关的事情,在控制器 FROM 和 TO 中也不做任何事情。
我已经实现了导航控制器委托,非常简单和处理实际动画的动画师类。
public class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
let animator = Animator()
public func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if operation == UINavigationControllerOperation.Pop {
return self.animator
}
return nil
}
}
动画师类:
class Animator: NSObject, UIViewControllerAnimatedTransitioning {
public func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
return 0.33
}
public func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
transitionContext.containerView().addSubview(toVC.view)
toVC.view.alpha = 0
UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: { () -> Void in
fromVC.view.transform = CGAffineTransformMakeScale(0.1, 0.1)
toVC.view.alpha = 1
}) { (finished) -> Void in
fromVC.view.transform = CGAffineTransformIdentity
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
}
}
}
在 FROM 控制器中,我初始化导航委托
override public func viewDidLoad() {
super.viewDidLoad()
// ... some other code here
self.navigationController?.delegate = NavigationControllerDelegate()
}
一切都编译并运行,直到我点击(收集)单元格并想要移动到 TO 控制器。此时应用程序崩溃并出现错误:-[CALayer navigationController:animationControllerForOperation:fromViewController:toViewController:]: unrecognized selector sent to instance 0x7fc72e53e970
我添加了一个断点,最终进入sizzled_pushViewController:animated:
了 UINavigationController 的方法。老实说,我不知道如何处理这个问题。