2014-05-08 37 views

回答

15

可以使用CATransition作爲證明this answer

CATransition* transition = [CATransition animation]; 
transition.duration = 0.5; 
transition.type = kCATransitionFade; 
[self.navigationController.view.layer addAnimation:transition forKey:nil]; 
[self.navigationController pushViewController:viewController animated:NO]; 
+1

這不完全相同的事情。它溶入,但它仍然看起來像滑入。 – BennyTheNerd

+0

如果將動畫設置爲NO,則滑動(推動)動畫將被禁用,因此它可以按預期工作 – David

1

SWIFT 3 - 截至11月,2017年

let transition = CATransition() 
transition.duration = 0.5 
transition.type = kCATransitionFade 
self.navigationController?.view.layer.add(transition, forKey:nil) 
0

的UINavigationControllerDelegate協議有可能返回一個自定義的方法UIViewControllerAnimatedTransitioning對象,它將控制參與轉換的兩個視圖控制器之間的動畫。

創建一個動畫類,以控制跨解散過渡:

class Animator: NSObject, UIViewControllerAnimatedTransitioning { 

    let animationDuration = 0.25 

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
     return animationDuration 
    } 

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
     let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) 
     toVC?.view.alpha = 0.0 
     let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) 
     transitionContext.containerView.addSubview(fromVC!.view) 
     transitionContext.containerView.addSubview(toVC!.view) 

     UIView.animate(withDuration: animationDuration, animations: { 
      toVC?.view.alpha = 1.0 
     }) { (completed) in 
      fromVC?.view.removeFromSuperview() 
      transitionContext.completeTransition(!transitionContext.transitionWasCancelled) 
     } 
    } 
} 

而在你UINavigationControllerDelegate它提供:

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
    return Animator() 
} 

這裏是一個更深入的教程:http://blog.rinatkhanov.me/ios/transitions.html

相關問題