给控制器添加全屏右滑返回手势

背景:系统自带的返回必须是原生的导航条,且不能添加自定义的 baritem,但是一般都是需要自定义按钮的,因此失去右滑的手势。这个功能还是非常实用的,我们要实现的是全屏右滑来实现返回,毕竟手机越来越大,还是挺有必要的。

原理:继承系统导航控制器,添加全屏手势方法,关闭系统的边缘手势,防止冲突。

具体代码:

1.添加手势

@interfaceZNavViewController :UINavigationController  

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

idtarget =self.interactivePopGestureRecognizer.delegate;

SELhandler =NSSelectorFromString(@"handleNavigationTransition:");

//获取添加系统边缘触发手势的view

UIView*targetView =self.interactivePopGestureRecognizer.view;

//创建pan手势作用全屏范围

UIPanGestureRecognizer*fullScreenGes = [[UIPanGestureRecognizeralloc]initWithTarget:targetaction:handler];

fullScreenGes.delegate=self;

[targetViewaddGestureRecognizer:fullScreenGes];

//关闭边缘触发手势防止冲突

[self.interactivePopGestureRecognizersetEnabled:NO];

}

2.解决冲突

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer

{

//解决与左滑手势冲突

CGPointtranslation = [(UIPanGestureRecognizer*)gestureRecognizertranslationInView:gestureRecognizer.view];

if(translation.x<=0) {

returnNO;

}

returnself.childViewControllers.count==1?NO:YES;

}

你可能感兴趣的:(给控制器添加全屏右滑返回手势)