如果我只是调用push方法:
[self.navigationController pushViewController:viewController animated:YES];
然后它使用推动画。如何更改它以使用交叉溶解动画,就像我可以使用模态segue?
您可以使用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];
我使用扩展名来轻松重用:
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时,您仍会看到标准的“推送”动画(从右到左)。
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()
}
SWIFT 3 - 截至2018年4月
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionFade
self.navigationController?.view.layer.add(transition, forKey:nil)
您可以像这样设置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];