MyUINavigationController.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // MyUINavigationController.m
  3. // jiaPeiC
  4. //
  5. // Created by Echopentakill on 16/12/13.
  6. // Copyright © 2016年 JCZ. All rights reserved.
  7. //
  8. #import "MyUINavigationController.h"
  9. @interface MyUINavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>
  10. @end
  11. @implementation MyUINavigationController
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. __weak MyUINavigationController *weakSelf = self;
  15. if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
  16. {
  17. self.interactivePopGestureRecognizer.delegate = weakSelf;
  18. self.delegate = weakSelf;
  19. }
  20. }
  21. //在转场/过渡的时候禁用 interactivePopGestureRecognizer当用户在转场的时候触发一个后退手势,则各种事件又凑一块了.导航栈内又成了混乱的.我的解决办法是,转场效果的过程中禁用手势识别,当新的视图控制器加载完成后再启用.再次建议使用UINavigationController的子类操作
  22. -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  23. {
  24. if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
  25. self.interactivePopGestureRecognizer.enabled = NO;
  26. [super pushViewController:viewController animated:animated];
  27. }
  28. #pragma mark UINavigationControllerDelegate
  29. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
  30. {
  31. // Enable the gesture again once the new controller is shown
  32. if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
  33. self.interactivePopGestureRecognizer.enabled = YES;
  34. }
  35. //
  36. //解决左滑手势冲突和不灵敏的问题
  37. -(UIViewController *)popViewControllerAnimated:(BOOL)animated {
  38. return [super popViewControllerAnimated:YES];
  39. }
  40. #pragma mark UIGestureRecognizerDelegate
  41. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
  42. if ([self.childViewControllers count] == 1) {
  43. return NO;
  44. }
  45. return YES;
  46. }
  47. // 我们差不多能猜到是因为手势冲突导致的,那我们就先让 ViewController 同时接受多个手势吧
  48. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
  49. return YES;
  50. }
  51. //运行试一试,两个问题同时解决,不过又发现了新问题,手指在滑动的时候,被 pop 的 ViewController 中的 UIScrollView 会跟着一起滚动,这个效果看起来就很怪(知乎日报现在就是这样的效果),而且也不是原始的滑动返回应有的效果,那么就让我们继续用代码来解决吧
  52. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  53. {
  54. return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
  55. }
  56. - (void)didReceiveMemoryWarning {
  57. [super didReceiveMemoryWarning];
  58. // Dispose of any resources that can be recreated.
  59. }
  60. /*
  61. #pragma mark - Navigation
  62. // In a storyboard-based application, you will often want to do a little preparation before navigation
  63. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  64. // Get the new view controller using [segue destinationViewController].
  65. // Pass the selected object to the new view controller.
  66. }
  67. */
  68. @end