一、手势右滑返回效果失效
在我们经常使用的APP中,已经习惯右滑返回这个效果,可是我发现我们的APP中这个功能失效了,只能点击左上角的返回按钮才能执行返回这个效果。后来查了一下发现,导致这个问题的原因是因为我们自己自定义了左上角的leftBarButtonItem,我们自定义了这个BarButtonItem使得系统不能捕获pop手势了。
解决方法:
创建一个UINavigationController的子类LGJBaseNavController,该类@interface LGJBaseNavController (),所有的关于导航控制器的操作都在这个类里面操作。
1 设置手势的delegate为self,导航控制器的delegate也为self。
- (void)viewDidLoad {
[super viewDidLoad];
__weak LGJBaseNavController *weakSelf = self;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.delegate = weakSelf;
self.delegate = weakSelf;
}
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
return [super popViewControllerAnimated:animated];
}
2 在转场/过渡的时候禁用 interactivePopGestureRecognizer当用户在转场的时候触发一个后退手势,这时候容易各种其他事件也会被唤醒,导航栈或边混乱。那么在转场效果的过程中禁用手势识别,当新的视图控制器加载完成后再启用。
#pragma mark - UINavigationControllerDelegate
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = NO;
}
//设置返回按钮
if (self.viewControllers.count > 0) {
viewController.navigationItem.leftBarButtonItem = [self backButtonItem];
}
[super pushViewController:viewController animated:animated];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = YES;
}
}
3 使navigationcontroller中第一个控制器不响应右滑pop手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([self.childViewControllers count] == 1) {
return NO;
}
return YES;
}
4 解决多个手势冲突 同时接受多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
5 解决在手指滑动时候,被pop的viewController中的UIscrollView会跟着一起滚动
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}
关于这个处理复杂手势冲突的方法有几个类似的手势代理方法:
//手指触摸屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
//开始进行手势识别时调用的方法,返回NO则结束,不再触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
//是否支持多时候触发,返回YES,则可以多个手势一起触发方法,返回NO则为互斥
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
//下面这个两个方法也是用来控制手势的互斥执行的
//这个方法返回YES,第一个手势和第二个互斥时,第一个会失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
//这个方法返回YES,第一个和第二个互斥时,第二个会失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
可以将这几个方法写在一个UINavigationController的子类里面,然后在AppDelegate中设置self.window.rootViewController = [[LGJBaseNavController alloc] initWithRootViewController:vc1];可以处理全局的导航控制器关于这个右滑手势返回失效的问题,我以上写的这些方法直接写在里面就可以。另外说一句,在这个LGJBaseNavController中可以设置导航栏的一些自定义样式:比如这种样式就可以在LGJBaseNavController初始化中设置:
navBar.png
+ (void)initialize {
//bar样式
UINavigationBar *bar = [UINavigationBar appearance];
[bar setBarStyle:UIBarStyleDefault];
[bar setBarTintColor:[UIColor blackColor]];
[bar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
//barButton样式
UIBarButtonItem *item = [UIBarButtonItem appearance];
//Normal
NSMutableDictionary *textAtts = [NSMutableDictionary dictionary];
textAtts[NSForegroundColorAttributeName] = [UIColor orangeColor];
textAtts[NSFontAttributeName] = [UIFont systemFontOfSize:13];
[item setTitleTextAttributes:textAtts forState:UIControlStateNormal];
//不可用状态
NSMutableDictionary *disableTextAtts = [NSMutableDictionary dictionary];
disableTextAtts[NSForegroundColorAttributeName] = [UIColor colorWithRed:123/255.0 green:123/255.0 blue:123/255.0 alpha:1];
disableTextAtts[NSFontAttributeName] = [UIFont systemFontOfSize:13];
[item setTitleTextAttributes:disableTextAtts forState:UIControlStateDisabled];
}
二、隐藏NavigationBar返回时,上面会有空缺
这个应该也是我们经常见的效果,比如vc1跳转vc2,vc2的导航栏是隐藏的,当从vc2返回vc1时,在这个过程中,放慢看上面会有缺失一块,放快了看就是闪屏,这个效果对用户也是不友好的,这个解决方法比较简单。就是在次级viewControleller中在设置setNavigationBarHidden时这样设置: <重要的是后面的animated参数>
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
注:此文章为转载,原文地址:http://www.cocoachina.com/ios/20170602/19418.html