8 
  9 一、手势右滑返回效果失效
 10 
 11 在我们经常使用的APP中,已经习惯右滑返回这个效果,可是我发现我们的APP中这个功能失效了,只能点击左上角的返回按钮才能执行返回这个效果。后来查了一下发现,导致这个问题的原因是因为我们自己自定义了左上角的leftBarButtonItem,我们自定义了这个BarButtonItem使得系统不能捕获pop手势了。
 12 
 13 解决方法:
 14 
 15 创建一个UINavigationController的子类LGJBaseNavController,该类@interface LGJBaseNavController (),所有的关于导航控制器的操作都在这个类里面操作。
 16 
 17 1 设置手势的delegate为self,导航控制器的delegate也为self。
 18 
 19 - (void)viewDidLoad {
 20     [super viewDidLoad];
 21  
 22     __weak LGJBaseNavController *weakSelf = self;
 23     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
 24         self.interactivePopGestureRecognizer.delegate = weakSelf;
 25         self.delegate = weakSelf;
 26     }
 27 }
 28  
 29 - (UIViewController *)popViewControllerAnimated:(BOOL)animated {
 30     return [super popViewControllerAnimated:animated];
 31 }
 32 2 在转场/过渡的时候禁用 interactivePopGestureRecognizer当用户在转场的时候触发一个后退手势,这时候容易各种其他事件也会被唤醒,导航栈或边混乱。那么在转场效果的过程中禁用手势识别,当新的视图控制器加载完成后再启用。
 33 
 34 #pragma mark - UINavigationControllerDelegate
 35 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
 36     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
 37         self.interactivePopGestureRecognizer.enabled = NO;
 38     }
 39     //设置返回按钮
 40     if (self.viewControllers.count > 0) {
 41         viewController.navigationItem.leftBarButtonItem = [self backButtonItem];
 42     }
 43     [super pushViewController:viewController animated:animated];
 44 }
 45  
 46 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
 47     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
 48         self.interactivePopGestureRecognizer.enabled = YES;
 49     }
 50 }
 51 3 使navigationcontroller中第一个控制器不响应右滑pop手势
 52 
 53 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
 54     if ([self.childViewControllers count] == 1) {
 55         return NO;
 56     }
 57     return YES;
 58 }
 59 4 解决多个手势冲突 同时接受多个手势
 60 
 61 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
 62     return YES;
 63 }
 64 5 解决在手指滑动时候,被pop的viewController中的UIscrollView会跟着一起滚动
 65 
 66 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
 67     return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
 68 }
 69 关于这个处理复杂手势冲突的方法有几个类似的手势代理方法:
 70 
 71 //手指触摸屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等
 72 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
 73 //开始进行手势识别时调用的方法,返回NO则结束,不再触发手势
 74 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
 75 //是否支持多时候触发,返回YES,则可以多个手势一起触发方法,返回NO则为互斥
 76 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
 77 //下面这个两个方法也是用来控制手势的互斥执行的
 78 //这个方法返回YES,第一个手势和第二个互斥时,第一个会失效
 79 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
 80 //这个方法返回YES,第一个和第二个互斥时,第二个会失效
 81 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
 82 可以将这几个方法写在一个UINavigationController的子类里面,然后在AppDelegate中设置self.window.rootViewController = [[LGJBaseNavController alloc] initWithRootViewController:vc1];可以处理全局的导航控制器关于这个右滑手势返回失效的问题,我以上写的这些方法直接写在里面就可以。另外说一句,在这个LGJBaseNavController中可以设置导航栏的一些自定义样式:比如这种样式就可以在LGJBaseNavController初始化中设置:
 83 
 84 668737-e1c9f390f405bd70.png
 85 
 86 navBar.png
 87 
 88 
 89 + (void)initialize {
 90  
 91     //bar样式
 92     UINavigationBar *bar = [UINavigationBar appearance];
 93     [bar setBarStyle:UIBarStyleDefault];
 94     [bar setBarTintColor:[UIColor blackColor]];
 95  
 96     [bar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
 97  
 98     //barButton样式
 99     UIBarButtonItem *item = [UIBarButtonItem appearance];
100  
101     //Normal
102     NSMutableDictionary *textAtts = [NSMutableDictionary dictionary];
103     textAtts[NSForegroundColorAttributeName] = [UIColor orangeColor];
104     textAtts[NSFontAttributeName] = [UIFont systemFontOfSize:13];
105     [item setTitleTextAttributes:textAtts forState:UIControlStateNormal];
106  
107     //不可用状态
108     NSMutableDictionary *disableTextAtts = [NSMutableDictionary dictionary];
109     disableTextAtts[NSForegroundColorAttributeName] = [UIColor colorWithRed:123/255.0 green:123/255.0 blue:123/255.0 alpha:1];
110     disableTextAtts[NSFontAttributeName] = [UIFont systemFontOfSize:13];
111     [item setTitleTextAttributes:disableTextAtts forState:UIControlStateDisabled];
112 }
113 二、隐藏NavigationBar返回时,上面会有空缺
114 
115 这个应该也是我们经常见的效果,比如vc1跳转vc2,vc2的导航栏是隐藏的,当从vc2返回vc1时,在这个过程中,放慢看上面会有缺失一块,放快了看就是闪屏,这个效果对用户也是不友好的,这个解决方法比较简单。就是在次级viewControleller中在设置setNavigationBarHidden时这样设置: <重要的是后面的animated参数>
116 
117 
118 - (void)viewWillAppear:(BOOL)animated {
119     [super viewWillAppear:animated];
120     [self.navigationController setNavigationBarHidden:YES animated:animated];
121 }
122  
123 - (void)viewWillDisappear:(BOOL)animated {
124     [super viewWillDisappear:animated];
125     [self.navigationController setNavigationBarHidden:NO animated:animated];
126 }

 

posted on 2017-06-02 15:13  Yevgeni  阅读(167)  评论(0编辑  收藏  举报