方案一:开启使用系统自带的侧滑返回
iOS7之后系统提供了侧滑手势(interactivePopGestureRecognizer),即从屏幕左侧边缘滑起会pop回导航控制器栈的上个viewController。不过如果你自定义了返回按钮,系统自带的侧滑返回功能会失效。此时需要添加下面的代码解决:
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
缺点:
必须从屏幕边缘左侧滑起才会触发;
一旦自定义导航控制器或者自定义返回按钮,就会失效。
方案二:极其简单取巧的方法
iOS7之后是有侧滑返回手势功能的。注意,也就是说系统已经定义了一种手势,并且给这个手势已经添加了一个触发方法(重点)。但是,系统的这个手势的触发条件是必须从屏幕左边缘开始滑动。我们取巧的方法是自己写一个支持全屏滑动的手势,而其触发方法系统已经有,没必要自己实现pop的动画,所以直接就把系统的触发处理方法作为我们自己定义的手势的处理方法。
#import "RootNavigationController.h"
@interface RootNavigationController ()<UIGestureRecognizerDelegate>
@end
@implementation RootNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBar.layoutMargins = UIEdgeInsetsZero;
// 全屏右滑,自己添加手势,采用系统的代理,以及调用系统的方法;
//系统的私有API
SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:internalAction];
// 设置代理,处理根控制器下的业务
pan.delegate = self;
[self.view addGestureRecognizer:pan];
// 清空原生手势,以此避免不必要bug
self.interactivePopGestureRecognizer.enabled = NO;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
if (self.topViewController ) {
CGPoint tPoint = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:gestureRecognizer.view];
if (tPoint.x >= 0) {
CGFloat y = fabs(tPoint.y);
CGFloat x = fabs(tPoint.x);
CGFloat af = 30.0f/180.0f * M_PI;
CGFloat tf = tanf(af);
if ((y/x) <= tf) {
return YES;
}
return NO;
}else{
return NO;
}
}
}
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return self.childViewControllers.count > 1;
}
@end
方案三:完全自定义
这是苹果官方在WWDC上提倡的方法,灵活性高。可以高度自定义push和pop转场动画。
这种方法需要我们彻底实现侧滑返回,那我们的思路就是:
- 先给view添加一个UIPanGestureRecognizer手势;
- 再自定义该手势的触发方法,该方法里实现了侧滑。
- 先创建一个BaseViewController,给该控制器的view添加拖动手势;
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.delegate = self; // 设置navigationController的代理为self,并实现其代理方法
self.view.userInteractionEnabled = YES;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(backHandle:)];
[self.view addGestureRecognizer:panGesture];
}
- (void)backHandle:(UIPanGestureRecognizer *)recognizer
{
[self customControllerPopHandle:recognizer];
}
2.侧滑手势会触发这个回调方法;
- (void)customControllerPopHandle:(UIPanGestureRecognizer *)recognizer
{
if(self.navigationController.childViewControllers.count == 1)
{
return;
}
// _interactiveTransition就是代理方法2返回的交互对象,我们需要更新它的进度来控制POP动画的流程。(以手指在视图中的位置与屏幕宽度的比例作为进度)
CGFloat process = [recognizer translationInView:self.view].x/self.view.bounds.size.width;
process = MIN(1.0, MAX(0.0, process));
if(recognizer.state == UIGestureRecognizerStateBegan)
{
// 此时,创建一个UIPercentDrivenInteractiveTransition交互对象,来控制整个过程中动画的状态
_interactiveTransition = [[UIPercentDrivenInteractiveTransition alloc] init];
[self.navigationController popViewControllerAnimated:YES];
}
else if(recognizer.state == UIGestureRecognizerStateChanged)
{
[_interactiveTransition updateInteractiveTransition:process]; // 更新手势完成度
}
else if(recognizer.state == UIGestureRecognizerStateEnded ||recognizer.state == UIGestureRecognizerStateCancelled)
{
// 手势结束时,若进度大于0.5就完成pop动画,否则取消
if(process > 0.5)
{
[_interactiveTransition finishInteractiveTransition];
}
else
{
[_interactiveTransition cancelInteractiveTransition];
}
_interactiveTransition = nil;
}
}
3.实现UINavigationControllerDelegate的两个协议方法,分别返回自定义动画需要的两个重要对象;
// 代理方法1:
// 返回一个实现了UIViewControllerAnimatedTransitioning协议的对象 ,即完成转场动画的对象
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if(operation == UINavigationControllerOperationPop) // 若operation是pop,就返回我们自定义的转场动画对象
{
return [[POPAnimation alloc] init];
}
return nil;
}
// 代理方法2
// 返回一个实现了UIViewControllerInteractiveTransitioning协议的对象,即完成动画交互(动画进度)的对象
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
if([animationController isKindOfClass:[POPAnimation class]])
{
return _interactiveTransition;
}
return nil;
}
4.创建一个自定义动画类:POPAnimation。这是自定义动画的核心
自定义动画类,即一个实现了UIViewControllerAnimatedTransitioning协议的类。
实现该协议的两个方法,一个返回动画执行时间,一个自定义动画。
#import "POPAnimation.h"
@interface POPAnimation ()
@end
@implementation POPAnimation
// 实现两个协议的方法
// 返回动画执行的时间
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.25;
}
//
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
__block UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; // 动画来自哪个vc
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; // 转场到哪个vc
// 转场动画是两个控制器视图的动画,需要一个containerView作为“舞台”
UIView *containerView = [transitionContext containerView];
[containerView insertSubview:toVC.view belowSubview:fromVC.view];
NSTimeInterval duration = [self transitionDuration:transitionContext]; // 获取动画执行时间(实现的协议方法)
// 执行动画,让fromVC的view移动到屏幕最右侧
[UIView animateWithDuration:duration animations:^{
fromVC.view.transform = CGAffineTransformMakeTranslation([UIScreen mainScreen].bounds.size.width, 0);
} completion:^(BOOL finished) {
// 当动画执行完时,这个方法必须要调用,否则系统会认为你的其余操作都在动画执行过程中
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
@end
方案四:用第三方
把里面的分类直接拖进项目就可以
https://github.com/forkingdog/FDFullscreenPopGesture