酷店

UINavigationController+FDFullscreenPopGesture.m 11KB

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