IOS全局返回以及侧边返回AND防止跟视图卡死大全

第一步:先来看一下侧滑返回

/**

//1.遵守代理

UINavigationBarDelegate

//2.实现俩个方法

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {

//只有一个控制器的时候禁止手势,防止卡死现象

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

self.interactivePopGestureRecognizer.enabled = NO;

}

if (self.childViewControllers.count > 1) {

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

self.interactivePopGestureRecognizer.enabled = YES;

}

}

return YES;

}

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item {

//只有一个控制器的时候禁止手势,防止卡死现象

if (self.childViewControllers.count == 1) {

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

self.interactivePopGestureRecognizer.enabled = NO;

}

}

}

*/

第二步:再来看一下全局返回\

都是继承于UINavigationController

1.

遵守代理

UIGestureRecognizerDelegate

2.在viewDidLoad中创建pan手势

- (void)viewDidLoad {

[superviewDidLoad];

self.navigationBar.barTintColor= [UIColorpurpleColor];

// 添加手势

UIPanGestureRecognizer* pan = [[UIPanGestureRecognizeralloc]initWithTarget:self.interactivePopGestureRecognizer.delegateaction:@selector(handleNavigationTransition:)];

pan.delegate=self;

[self.viewaddGestureRecognizer:pan];

// 系统自带手势失效

self.interactivePopGestureRecognizer.enabled=NO;

}

3.实现代理方法并做判断

#pragma mark --- UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch{

// 如果不是根视图控制器就让侧滑手势有效,否则失效

if(self.childViewControllers.count>1) {

returnYES;

}else{

returnNO;

}

}

你可能感兴趣的:(IOS全局返回以及侧边返回AND防止跟视图卡死大全)