更新到正确的版本,因为得到了答案。
我希望在导航视图控制器弹出/推送时淡入/淡出动画,然后我实现BaseViewController
:
class BaseViewController: UIViewController, UINavigationControllerDelegate {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.delegate = self
}
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if (operation == UINavigationControllerOperation.Push) {
return FadeInAnimator()
}
if (operation == UINavigationControllerOperation.Pop) {
return FadeOutAnimator()
}
return nil;
}
}
和FadeInAnimator
,FadeOutAnimator
:
class FadeInAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
transitionContext.containerView()?.addSubview(toViewController!.view)
toViewController?.view.alpha = 0.0
UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: { () -> Void in
toViewController?.view.alpha = 1.0
}) { (finished) -> Void in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
}
}
}
class FadeOutAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
transitionContext.containerView()?.insertSubview((toViewController?.view)!, belowSubview: (fromViewController?.view)!)
UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: { () -> Void in
fromViewController?.view.alpha = 0.0
}) { (finished) -> Void in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
}
}
然后,我添加了2个视图控制器并嵌入了导航视图控制器,ViewControllerA
和ViewControllerB
:
class ViewControllerA: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func pressButton(sender: UIButton) {
if let vc = create(ViewControllerB) {
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
class ViewControllerB: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
按ViewControllerA
上的按钮将创建并跳转到ViewControllerB
,然后按导航栏项目"返回"在ViewControllerB
上将返回ViewControllerA
。
但它不起作用,按下按钮,ViewControllerA
显示ViewControllerB
,然后再次显示ViewControllerA
。
有什么建议吗?我的xcode是7.1并在iPad Air 2模拟器上运行。
答案 0 :(得分:1)
在动画完成块的FadeInAnimator中
更改此
transitionContext.completeTransition(transitionContext.transitionWasCancelled())
到
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())