NavigationDemo
展示三种popViewController的效果,例如淘宝、京东的“整体返回”效果
效果图:
实现思路:
1. 创建Pan手势识别器
2. 实现手势的相应事件
3. 创建截图所需的backgroundView和blackMask作为遮罩,以及存放截图所需数组的类
4. 在push之前截图并保存
5. 重写常用的pop方法,在pop之前删除相应的截图
override func popViewController(animated: Bool) -> UIViewController?
override func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]?
override func popToRootViewController(animated: Bool) -> [UIViewController]?
到此处可以实现手势整体返回的效果,要实现点击返回按钮也能整体返回,需要自定义返回动画。实现协议UIViewControllerAnimatedTransitioning
。
6. 让navigationController遵守UINavigationControllerDelegate并实现以下方法,在方法中可以根据operation判断需要自定义的类型(pop/push)
func navigationController(_ navigationController: UINavigationController,
animationControllerFor operation: UINavigationControllerOperation,
from fromVC: UIViewController,
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
7. 使用一个类实现UIViewControllerAnimatedTransitioning协议
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
//定义动画
func animateTransition(using transitionContext: UIViewControllerContextTransitioning)