UINavigationController+FDFullscreenPopGesture.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // Ignore 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 when the beginning location is beyond max allowed initial distance to left edge.
  40. CGPoint beginningLocation = [gestureRecognizer locationInView:gestureRecognizer.view];
  41. CGFloat maxAllowedInitialDistance = topViewController.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge;
  42. if (maxAllowedInitialDistance > 0 && beginningLocation.x > maxAllowedInitialDistance) {
  43. return NO;
  44. }
  45. // Ignore pan gesture when the navigation controller is currently in transition.
  46. if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) {
  47. return NO;
  48. }
  49. // Prevent calling the handler when the gesture begins in an opposite direction.
  50. CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
  51. if (translation.x <= 0) {
  52. return NO;
  53. }
  54. return YES;
  55. }
  56. @end
  57. typedef void (^_FDViewControllerWillAppearInjectBlock)(UIViewController *viewController, BOOL animated);
  58. @interface UIViewController (FDFullscreenPopGesturePrivate)
  59. @property (nonatomic, copy) _FDViewControllerWillAppearInjectBlock fd_willAppearInjectBlock;
  60. @end
  61. @implementation UIViewController (FDFullscreenPopGesturePrivate)
  62. + (void)load
  63. {
  64. static dispatch_once_t onceToken;
  65. dispatch_once(&onceToken, ^{
  66. Class class = [self class];
  67. SEL originalSelector = @selector(viewWillAppear:);
  68. SEL swizzledSelector = @selector(fd_viewWillAppear:);
  69. Method originalMethod = class_getInstanceMethod(class, originalSelector);
  70. Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
  71. BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
  72. if (success) {
  73. class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  74. } else {
  75. method_exchangeImplementations(originalMethod, swizzledMethod);
  76. }
  77. });
  78. }
  79. - (void)fd_viewWillAppear:(BOOL)animated
  80. {
  81. // Forward to primary implementation.
  82. [self fd_viewWillAppear:animated];
  83. if (self.fd_willAppearInjectBlock) {
  84. self.fd_willAppearInjectBlock(self, animated);
  85. }
  86. }
  87. - (_FDViewControllerWillAppearInjectBlock)fd_willAppearInjectBlock
  88. {
  89. return objc_getAssociatedObject(self, _cmd);
  90. }
  91. - (void)setFd_willAppearInjectBlock:(_FDViewControllerWillAppearInjectBlock)block
  92. {
  93. objc_setAssociatedObject(self, @selector(fd_willAppearInjectBlock), block, OBJC_ASSOCIATION_COPY_NONATOMIC);
  94. }
  95. @end
  96. @implementation UINavigationController (FDFullscreenPopGesture)
  97. + (void)load
  98. {
  99. // Inject "-pushViewController:animated:"
  100. static dispatch_once_t onceToken;
  101. dispatch_once(&onceToken, ^{
  102. Class class = [self class];
  103. SEL originalSelector = @selector(pushViewController:animated:);
  104. SEL swizzledSelector = @selector(fd_pushViewController:animated:);
  105. Method originalMethod = class_getInstanceMethod(class, originalSelector);
  106. Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
  107. BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
  108. if (success) {
  109. class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  110. } else {
  111. method_exchangeImplementations(originalMethod, swizzledMethod);
  112. }
  113. });
  114. }
  115. - (void)fd_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  116. {
  117. if (![self.interactivePopGestureRecognizer.view.gestureRecognizers containsObject:self.fd_fullscreenPopGestureRecognizer]) {
  118. // Add our own gesture recognizer to where the onboard screen edge pan gesture recognizer is attached to.
  119. [self.interactivePopGestureRecognizer.view addGestureRecognizer:self.fd_fullscreenPopGestureRecognizer];
  120. // Forward the gesture events to the private handler of the onboard gesture recognizer.
  121. NSArray *internalTargets = [self.interactivePopGestureRecognizer valueForKey:@"targets"];
  122. id internalTarget = [internalTargets.firstObject valueForKey:@"target"];
  123. SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
  124. self.fd_fullscreenPopGestureRecognizer.delegate = self.fd_popGestureRecognizerDelegate;
  125. [self.fd_fullscreenPopGestureRecognizer addTarget:internalTarget action:internalAction];
  126. // Disable the onboard gesture recognizer.
  127. self.interactivePopGestureRecognizer.enabled = NO;
  128. }
  129. // Handle perferred navigation bar appearance.
  130. [self fd_setupViewControllerBasedNavigationBarAppearanceIfNeeded:viewController];
  131. // Forward to primary implementation.
  132. if (![self.viewControllers containsObject:viewController]) {
  133. [self fd_pushViewController:viewController animated:animated];
  134. }
  135. }
  136. - (void)fd_setupViewControllerBasedNavigationBarAppearanceIfNeeded:(UIViewController *)appearingViewController
  137. {
  138. if (!self.fd_viewControllerBasedNavigationBarAppearanceEnabled) {
  139. return;
  140. }
  141. __weak typeof(self) weakSelf = self;
  142. _FDViewControllerWillAppearInjectBlock block = ^(UIViewController *viewController, BOOL animated) {
  143. __strong typeof(weakSelf) strongSelf = weakSelf;
  144. if (strongSelf) {
  145. [strongSelf setNavigationBarHidden:viewController.fd_prefersNavigationBarHidden animated:animated];
  146. }
  147. };
  148. // Setup will appear inject block to appearing view controller.
  149. // Setup disappearing view controller as well, because not every view controller is added into
  150. // stack by pushing, maybe by "-setViewControllers:".
  151. appearingViewController.fd_willAppearInjectBlock = block;
  152. UIViewController *disappearingViewController = self.viewControllers.lastObject;
  153. if (disappearingViewController && !disappearingViewController.fd_willAppearInjectBlock) {
  154. disappearingViewController.fd_willAppearInjectBlock = block;
  155. }
  156. }
  157. - (_FDFullscreenPopGestureRecognizerDelegate *)fd_popGestureRecognizerDelegate
  158. {
  159. _FDFullscreenPopGestureRecognizerDelegate *delegate = objc_getAssociatedObject(self, _cmd);
  160. if (!delegate) {
  161. delegate = [[_FDFullscreenPopGestureRecognizerDelegate alloc] init];
  162. delegate.navigationController = self;
  163. objc_setAssociatedObject(self, _cmd, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  164. }
  165. return delegate;
  166. }
  167. - (UIPanGestureRecognizer *)fd_fullscreenPopGestureRecognizer
  168. {
  169. UIPanGestureRecognizer *panGestureRecognizer = objc_getAssociatedObject(self, _cmd);
  170. if (!panGestureRecognizer) {
  171. panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
  172. panGestureRecognizer.maximumNumberOfTouches = 1;
  173. objc_setAssociatedObject(self, _cmd, panGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  174. }
  175. return panGestureRecognizer;
  176. }
  177. - (BOOL)fd_viewControllerBasedNavigationBarAppearanceEnabled
  178. {
  179. NSNumber *number = objc_getAssociatedObject(self, _cmd);
  180. if (number) {
  181. return number.boolValue;
  182. }
  183. self.fd_viewControllerBasedNavigationBarAppearanceEnabled = YES;
  184. return YES;
  185. }
  186. - (void)setFd_viewControllerBasedNavigationBarAppearanceEnabled:(BOOL)enabled
  187. {
  188. SEL key = @selector(fd_viewControllerBasedNavigationBarAppearanceEnabled);
  189. objc_setAssociatedObject(self, key, @(enabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  190. }
  191. @end
  192. @implementation UIViewController (FDFullscreenPopGesture)
  193. - (BOOL)fd_interactivePopDisabled
  194. {
  195. return [objc_getAssociatedObject(self, _cmd) boolValue];
  196. }
  197. - (void)setFd_interactivePopDisabled:(BOOL)disabled
  198. {
  199. objc_setAssociatedObject(self, @selector(fd_interactivePopDisabled), @(disabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  200. }
  201. - (BOOL)fd_prefersNavigationBarHidden
  202. {
  203. return [objc_getAssociatedObject(self, _cmd) boolValue];
  204. }
  205. - (void)setFd_prefersNavigationBarHidden:(BOOL)hidden
  206. {
  207. objc_setAssociatedObject(self, @selector(fd_prefersNavigationBarHidden), @(hidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  208. }
  209. - (CGFloat)fd_interactivePopMaxAllowedInitialDistanceToLeftEdge
  210. {
  211. #if CGFLOAT_IS_DOUBLE
  212. return [objc_getAssociatedObject(self, _cmd) doubleValue];
  213. #else
  214. return [objc_getAssociatedObject(self, _cmd) floatValue];
  215. #endif
  216. }
  217. - (void)setFd_interactivePopMaxAllowedInitialDistanceToLeftEdge:(CGFloat)distance
  218. {
  219. SEL key = @selector(fd_interactivePopMaxAllowedInitialDistanceToLeftEdge);
  220. objc_setAssociatedObject(self, key, @(MAX(0, distance)), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  221. }
  222. @end