最近做项目的时候遇到个非常奇怪的情况,点击cell的时候会莫名的假死,将程序进入后台再切回来假死消失,但是还是不能进行操作。遇到这个问题的时候也真是一头雾水,找了很多资料,也试了很多办法,依然不起作用。后台仔细研究出现假死的情况发现每次在点击控制器最左边的时候就会出现假死情况,想了想是否和自带的侧滑手势有关,然后写了测试程序,发现果然是这个手势在作怪。
代码结构
代码结构就很简单了,根控制器是tabBarController,然后是两个navigationController,导航栏控制器根控制器为ChatViewController和ProfileViewController,点击ChatViewController的cell进入ChatMainViewController,然后在ChatMainController重写leftItem,这样的话侧滑手势不可用,但是只要重置侧滑手势代理即可解决
@implementation BZNavigationController
- (void)viewDidLoad{
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
}
@end
然后问题就开始出现了,此时点击 ChatViewController的cell最左边,在点击cell就出现了假死情况
之后任何操作都不可用,正是因为ChatViewController响应了侧滑手势,解决办法很简单,可以在导航栏控制器只有一个子控制器的时候将手势设为不可用即可,为了代码的扩展性,可以给控制器绑定一个侧滑手势是否可用的属性即可。
1 给UIViewController添加类目 UIViewController (Ex),增加两个方法
@interface UIViewController (Ex)
- (void)setInteractivePopGestureRecognizerEnable:(BOOL)enable;
- (BOOL)getInteractivePopGestureRecognizerEnable;
@end
.m实现这两个方法
#import "UIViewController+Ex.h"
#import <objc/runtime.h>
static char interactivePopGestureRecognizerEnableKey;
@implementation UIViewController (Ex)
- (void)setInteractivePopGestureRecognizerEnable:(BOOL)enable{
NSNumber *value = [NSNumber numberWithBool:enable];
objc_setAssociatedObject(self, &interactivePopGestureRecognizerEnableKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)getInteractivePopGestureRecognizerEnable{
NSNumber *value = objc_getAssociatedObject(self, &interactivePopGestureRecognizerEnableKey);
return [value boolValue];
}
@end
2 在每个控制器设定侧滑手势是否可用
3 实现 UINavigationControllerDelegate代理方法
@implementation BZNavigationController
- (void)viewDidLoad{
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
self.delegate = self;
}
#pragma mark - navi delegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
BOOL enable = [viewController getInteractivePopGestureRecognizerEnable];
self.interactivePopGestureRecognizer.enabled = enable;
}
@end
这样即可为每个控制器自定义是否可以相应侧滑手势了!