Navigation Transition

Modal Transition概述对自定义transition进行了简单概述,Using UIPercentDrivenInteractiveTransition介绍了可交互转场的基本实现思路。本文旨在总结实现自定义navigation转场的基本套路。

此外,还对自己写的相关demo做一个汇总。

Basic Methods Of Custom Navigation Transition

UINavigationController的基础上实现自定义custom navigation transition,只需要让自定义view controller遵循UINavigationControllerDelegate协议,如下:

class MainViewController: UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.delegate = self
// ...
}
}

默认情况下,UINavigationController#delegatenil,此时push和pop动画全部采用系统默认transition。

UINavigationControllerDelegate定义了6个方法,与transition直接相关的有两个:

// 为push和pop操作提供animation controller
func navigationController(navigationController: UINavigationController,
animationControllerForOperation operation: UINavigationControllerOperation,
fromViewController fromVC: UIViewController,
toViewController toVC: UIViewController)
-> UIViewControllerAnimatedTransitioning?
// 为push和pop操作提供interaction controller
func navigationController(
navigationController: UINavigationController,
interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning)
-> UIViewControllerInteractiveTransitioning?

站在这两个方法的角度来看,UINavigationControllerDelegate有点类似于UIViewControllerTransitioningDelegate,后者为transition动画提供animation controller和interaction controller,前者的作用也类似。

Some Demos

可交互的简单push/pop实现 – 仿知乎

Basic Interactive Custom Navigation Transition Demo

代码详见这里

如下是一些不错的开源custom navigation transition实现:

本文参考