如果我只需調用與推法:如何在iOS 7中的UINavigationController上推送視圖時交叉解散?
[self.navigationController pushViewController:viewController animated:YES];
然後使用推動畫。我如何改變它使用交叉溶解動畫,就像我可以使用模態賽格?
如果我只需調用與推法:如何在iOS 7中的UINavigationController上推送視圖時交叉解散?
[self.navigationController pushViewController:viewController animated:YES];
然後使用推動畫。我如何改變它使用交叉溶解動畫,就像我可以使用模態賽格?
可以使用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];
SWIFT 3 - 截至11月,2017年
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionFade
self.navigationController?.view.layer.add(transition, forKey:nil)
的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
這不完全相同的事情。它溶入,但它仍然看起來像滑入。 – BennyTheNerd
如果將動畫設置爲NO,則滑動(推動)動畫將被禁用,因此它可以按預期工作 – David