如果我只是用以下方法调用push方法:
[self.navigationController pushViewController:viewController animated:YES];
然后它使用推动画。如何更改它以使用交叉溶解动画,就像我可以使用模态segue?
答案 0 :(得分:18)
您可以使用this answer中所示的CATransition
:
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 :(得分:8)
我使用扩展程序以便于重复使用:
extension UINavigationController {
func fadeTo(_ viewController: UIViewController) {
let transition: CATransition = CATransition()
transition.duration = 0.3
transition.type = kCATransitionFade
view.layer.add(transition, forKey: nil)
pushViewController(viewController, animated: false)
}
}
注意动画是如何虚假的;当你将它设置为true时,你仍然可以看到标准的推送'动画(从右到左)。
答案 2 :(得分:6)
UINavigationControllerDelegate协议有一个方法可以返回一个自定义的UIViewControllerAnimatedTransitioning对象,该对象将控制转换中涉及的两个视图控制器之间的动画。
创建一个Animator类来控制交叉溶解过渡:
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
答案 3 :(得分:4)
SWIFT 3 - 截至2018年4月
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionFade
self.navigationController?.view.layer.add(transition, forKey:nil)
答案 4 :(得分:2)
您可以这样设置Push viewcontroller。
CATransition* transition = [CATransition animation];
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:@"kCATransition"];
[self.navigationController pushViewController:readerViewController animated:false];
您可以像这样设置Pop viewcontroller。
CATransition* transition = [CATransition animation];
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut];
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition
forKey:@"kCATransition"];
[self.navigationController popViewControllerAnimated:false];
答案 5 :(得分:0)
更新Swift 5
使用Application.WorksheetFunction.Match
代替prepare(for segue:)
pushViewController: