MyUINavigationController.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. NSArray *arr = @[@"ExerciseVC",@"LeeVideoVC"];
  33. for (NSString *str in arr) {
  34. Class cls =NSClassFromString(str);
  35. if ([viewController isMemberOfClass:cls]) {
  36. return;
  37. }
  38. }
  39. if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
  40. self.interactivePopGestureRecognizer.enabled = YES;
  41. }
  42. //
  43. //解决左滑手势冲突和不灵敏的问题
  44. -(UIViewController *)popViewControllerAnimated:(BOOL)animated {
  45. return [super popViewControllerAnimated:YES];
  46. }
  47. #pragma mark UIGestureRecognizerDelegate
  48. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
  49. if ([self.childViewControllers count] == 1) {
  50. return NO;
  51. }
  52. return YES;
  53. }
  54. // 我们差不多能猜到是因为手势冲突导致的,那我们就先让 ViewController 同时接受多个手势吧
  55. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
  56. return YES;
  57. }
  58. //运行试一试,两个问题同时解决,不过又发现了新问题,手指在滑动的时候,被 pop 的 ViewController 中的 UIScrollView 会跟着一起滚动,这个效果看起来就很怪(知乎日报现在就是这样的效果),而且也不是原始的滑动返回应有的效果,那么就让我们继续用代码来解决吧
  59. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  60. {
  61. return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
  62. }
  63. - (void)didReceiveMemoryWarning {
  64. [super didReceiveMemoryWarning];
  65. // Dispose of any resources that can be recreated.
  66. }
  67. @end