UINavigationController+FDFullscreenPopGesture.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2015-2016 forkingdog ( https://github.com/forkingdog )
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #import "UINavigationController+FDFullscreenPopGesture.h"
  23. #import <objc/runtime.h>
  24. @interface _FDFullscreenPopGestureRecognizerDelegate : NSObject <UIGestureRecognizerDelegate>
  25. @property (nonatomic, weak) UINavigationController *navigationController;
  26. @end
  27. @implementation _FDFullscreenPopGestureRecognizerDelegate
  28. - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
  29. {
  30. // Ignore when no view controller is pushed into the navigation stack.
  31. if (self.navigationController.viewControllers.count <= 1) {
  32. return NO;
  33. }
  34. // Disable when the active view controller doesn't allow interactive pop.
  35. UIViewController *topViewController = self.navigationController.viewControllers.lastObject;
  36. if (topViewController.fd_interactivePopDisabled) {
  37. return NO;
  38. }
  39. // Ignore pan gesture when the navigation controller is currently in transition.
  40. if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) {
  41. return NO;
  42. }
  43. // Prevent calling the handler when the gesture begins in an opposite direction.
  44. CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
  45. if (translation.x <= 0) {
  46. return NO;
  47. }
  48. return YES;
  49. }
  50. @end
  51. typedef void (^_FDViewControllerWillAppearInjectBlock)(UIViewController *viewController, BOOL animated);
  52. @interface UIViewController (FDFullscreenPopGesturePrivate)
  53. @property (nonatomic, copy) _FDViewControllerWillAppearInjectBlock fd_willAppearInjectBlock;
  54. @end
  55. @implementation UIViewController (FDFullscreenPopGesturePrivate)
  56. + (void)load
  57. {
  58. Method originalMethod = class_getInstanceMethod(self, @selector(viewWillAppear:));
  59. Method swizzledMethod = class_getInstanceMethod(self, @selector(fd_viewWillAppear:));
  60. method_exchangeImplementations(originalMethod, swizzledMethod);
  61. }
  62. - (void)fd_viewWillAppear:(BOOL)animated
  63. {
  64. // Forward to primary implementation.
  65. [self fd_viewWillAppear:animated];
  66. if (self.fd_willAppearInjectBlock) {
  67. self.fd_willAppearInjectBlock(self, animated);
  68. }
  69. }
  70. - (_FDViewControllerWillAppearInjectBlock)fd_willAppearInjectBlock
  71. {
  72. return objc_getAssociatedObject(self, _cmd);
  73. }
  74. - (void)setFd_willAppearInjectBlock:(_FDViewControllerWillAppearInjectBlock)block
  75. {
  76. objc_setAssociatedObject(self, @selector(fd_willAppearInjectBlock), block, OBJC_ASSOCIATION_COPY_NONATOMIC);
  77. }
  78. @end
  79. @implementation UINavigationController (FDFullscreenPopGesture)
  80. + (void)load
  81. {
  82. // Inject "-pushViewController:animated:"
  83. Method originalMethod = class_getInstanceMethod(self, @selector(pushViewController:animated:));
  84. Method swizzledMethod = class_getInstanceMethod(self, @selector(fd_pushViewController:animated:));
  85. method_exchangeImplementations(originalMethod, swizzledMethod);
  86. }
  87. - (void)fd_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  88. {
  89. if (![self.interactivePopGestureRecognizer.view.gestureRecognizers containsObject:self.fd_fullscreenPopGestureRecognizer]) {
  90. // Add our own gesture recognizer to where the onboard screen edge pan gesture recognizer is attached to.
  91. [self.interactivePopGestureRecognizer.view addGestureRecognizer:self.fd_fullscreenPopGestureRecognizer];
  92. // Forward the gesture events to the private handler of the onboard gesture recognizer.
  93. NSArray *internalTargets = [self.interactivePopGestureRecognizer valueForKey:@"targets"];
  94. id internalTarget = [internalTargets.firstObject valueForKey:@"target"];
  95. SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
  96. self.fd_fullscreenPopGestureRecognizer.delegate = self.fd_popGestureRecognizerDelegate;
  97. [self.fd_fullscreenPopGestureRecognizer addTarget:internalTarget action:internalAction];
  98. // Disable the onboard gesture recognizer.
  99. self.interactivePopGestureRecognizer.enabled = NO;
  100. }
  101. // Handle perferred navigation bar appearance.
  102. [self fd_setupViewControllerBasedNavigationBarAppearanceIfNeeded:viewController];
  103. // Forward to primary implementation.
  104. [self fd_pushViewController:viewController animated:animated];
  105. }
  106. - (void)fd_setupViewControllerBasedNavigationBarAppearanceIfNeeded:(UIViewController *)appearingViewController
  107. {
  108. if (!self.fd_viewControllerBasedNavigationBarAppearanceEnabled) {
  109. return;
  110. }
  111. __weak typeof(self) weakSelf = self;
  112. _FDViewControllerWillAppearInjectBlock block = ^(UIViewController *viewController, BOOL animated) {
  113. __strong typeof(weakSelf) strongSelf = weakSelf;
  114. if (strongSelf) {
  115. [strongSelf setNavigationBarHidden:viewController.fd_prefersNavigationBarHidden animated:animated];
  116. }
  117. };
  118. // Setup will appear inject block to appearing view controller.
  119. // Setup disappearing view controller as well, because not every view controller is added into
  120. // stack by pushing, maybe by "-setViewControllers:".
  121. appearingViewController.fd_willAppearInjectBlock = block;
  122. UIViewController *disappearingViewController = self.viewControllers.lastObject;
  123. if (disappearingViewController && !disappearingViewController.fd_willAppearInjectBlock) {
  124. disappearingViewController.fd_willAppearInjectBlock = block;
  125. }
  126. }
  127. - (_FDFullscreenPopGestureRecognizerDelegate *)fd_popGestureRecognizerDelegate
  128. {
  129. _FDFullscreenPopGestureRecognizerDelegate *delegate = objc_getAssociatedObject(self, _cmd);
  130. if (!delegate) {
  131. delegate = [[_FDFullscreenPopGestureRecognizerDelegate alloc] init];
  132. delegate.navigationController = self;
  133. objc_setAssociatedObject(self, _cmd, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  134. }
  135. return delegate;
  136. }
  137. - (UIPanGestureRecognizer *)fd_fullscreenPopGestureRecognizer
  138. {
  139. UIPanGestureRecognizer *panGestureRecognizer = objc_getAssociatedObject(self, _cmd);
  140. if (!panGestureRecognizer) {
  141. panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
  142. panGestureRecognizer.maximumNumberOfTouches = 1;
  143. objc_setAssociatedObject(self, _cmd, panGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  144. }
  145. return panGestureRecognizer;
  146. }
  147. - (BOOL)fd_viewControllerBasedNavigationBarAppearanceEnabled
  148. {
  149. NSNumber *number = objc_getAssociatedObject(self, _cmd);
  150. if (number) {
  151. return number.boolValue;
  152. }
  153. self.fd_viewControllerBasedNavigationBarAppearanceEnabled = YES;
  154. return YES;
  155. }
  156. - (void)setFd_viewControllerBasedNavigationBarAppearanceEnabled:(BOOL)enabled
  157. {
  158. SEL key = @selector(fd_viewControllerBasedNavigationBarAppearanceEnabled);
  159. objc_setAssociatedObject(self, key, @(enabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  160. }
  161. @end
  162. @implementation UIViewController (FDFullscreenPopGesture)
  163. - (BOOL)fd_interactivePopDisabled
  164. {
  165. return [objc_getAssociatedObject(self, _cmd) boolValue];
  166. }
  167. - (void)setFd_interactivePopDisabled:(BOOL)disabled
  168. {
  169. objc_setAssociatedObject(self, @selector(fd_interactivePopDisabled), @(disabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  170. }
  171. - (BOOL)fd_prefersNavigationBarHidden
  172. {
  173. return [objc_getAssociatedObject(self, _cmd) boolValue];
  174. }
  175. - (void)setFd_prefersNavigationBarHidden:(BOOL)hidden
  176. {
  177. objc_setAssociatedObject(self, @selector(fd_prefersNavigationBarHidden), @(hidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  178. }
  179. @end