UIViewController+TFPresent.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // UIViewController+TFPresent.m
  3. // trueface
  4. //
  5. // Created by Apple on 2019/10/12.
  6. // Copyright © 2019 jie. All rights reserved.
  7. //
  8. #import "UIViewController+TFPresent.h"
  9. #import "AppDelegate.h"
  10. @interface TFPresentAnimation:NSObject<UIViewControllerAnimatedTransitioning>
  11. @property(nonatomic,assign,getter=isPresentation)BOOL presentation;
  12. @property(nonatomic,assign)TFDirection direction;
  13. -(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext;
  14. -(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext;
  15. @end
  16. @implementation TFPresentAnimation
  17. +(instancetype)presentAnimationWithDirection:(TFDirection)direction isPresentation:(BOOL)isPresentation
  18. {
  19. TFPresentAnimation * presentAnimation = [[TFPresentAnimation alloc] init];
  20. presentAnimation.direction = direction;
  21. presentAnimation.presentation = isPresentation;
  22. return presentAnimation;
  23. }
  24. -(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext;
  25. {
  26. return 0.25f;
  27. }
  28. -(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  29. {
  30. //1
  31. UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  32. UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  33. //2
  34. CGRect backRect = transitionContext.containerView.bounds;
  35. if (self.presentation && toVC.view) {
  36. UIView * backView = [[UIView alloc] initWithFrame:backRect];
  37. backView.backgroundColor = [UIColor blackColor];
  38. backView.alpha = 0.5;
  39. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:fromVC action:@selector(TFDismissViewController)];
  40. [backView addGestureRecognizer:tap];
  41. [[transitionContext containerView] addSubview:backView];
  42. [[transitionContext containerView] addSubview:toVC.view];
  43. }
  44. CGSize finalSize = self.presentation? toVC.preferredContentSize:fromVC.preferredContentSize;
  45. CGFloat final_X=0,final_Y=0;
  46. switch (self.direction) {
  47. case TFDirectionCenter:
  48. {
  49. final_X = (CGRectGetWidth(backRect)-finalSize.width)/2.0;
  50. final_Y = (CGRectGetHeight(backRect)-finalSize.height)/2.0;
  51. }
  52. break;
  53. case TFDirectionTop:
  54. {
  55. final_X = (CGRectGetWidth(backRect)-finalSize.width)/2.0;
  56. final_Y = 0;
  57. }
  58. break;
  59. case TFDirectionLeft:
  60. {
  61. final_X = 0;
  62. final_Y = (CGRectGetHeight(backRect)-finalSize.height)/2.0;
  63. }
  64. break;
  65. case TFDirectionRight:
  66. {
  67. final_X = (CGRectGetWidth(backRect)-finalSize.width);
  68. final_Y = (CGRectGetHeight(backRect)-finalSize.height)/2.0;
  69. }
  70. break;
  71. case TFDirectionBottom:
  72. {
  73. final_X = (CGRectGetWidth(backRect)-finalSize.width)/2.0;
  74. final_Y = (CGRectGetHeight(backRect)-finalSize.height);
  75. }
  76. break;
  77. default:
  78. break;
  79. }
  80. CGRect finalRect = CGRectMake(final_X, final_Y, finalSize.width, finalSize.height);
  81. UIView * animationView = self.presentation?toVC.view:fromVC.view;
  82. NSInteger count = transitionContext.containerView.subviews.count;
  83. UIView *alphaView = transitionContext.containerView.subviews[count-2];
  84. alphaView.alpha = self.presentation?0:0.4f;
  85. //3
  86. switch (self.direction) {
  87. case TFDirectionCenter:
  88. {
  89. animationView.frame = self.presentation?CGRectOffset(finalRect, 0,[UIScreen mainScreen].bounds.size.height-final_Y):finalRect;
  90. }
  91. break;
  92. case TFDirectionTop:
  93. {
  94. animationView.frame = self.presentation?CGRectOffset(finalRect, 0,finalSize.height*(-1)):finalRect;
  95. }
  96. break;
  97. case TFDirectionLeft:
  98. {
  99. animationView.frame = self.presentation?CGRectOffset(finalRect, finalSize.width*(-1),0):finalRect;
  100. }
  101. break;
  102. case TFDirectionRight:
  103. {
  104. animationView.frame = self.presentation?CGRectOffset(finalRect, finalSize.width,0):finalRect;
  105. }
  106. break;
  107. case TFDirectionBottom:
  108. {
  109. animationView.frame = self.presentation?CGRectOffset(finalRect, 0, finalSize.height):finalRect;
  110. }
  111. break;
  112. default:
  113. break;
  114. }
  115. [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
  116. switch (self.direction) {
  117. case TFDirectionCenter:
  118. {
  119. animationView.frame = self.presentation?finalRect:CGRectOffset(finalRect, 0,[UIScreen mainScreen].bounds.size.height-final_Y);
  120. }
  121. break;
  122. case TFDirectionTop:
  123. {
  124. animationView.frame = self.presentation?finalRect:CGRectOffset(finalRect, 0,finalSize.height*(-1));
  125. }
  126. break;
  127. case TFDirectionLeft:
  128. {
  129. animationView.frame = self.presentation?finalRect:CGRectOffset(finalRect, finalSize.width*(-1),0);
  130. }
  131. break;
  132. case TFDirectionRight:
  133. {
  134. animationView.frame = self.presentation?finalRect:CGRectOffset(finalRect, finalSize.width,0);
  135. }
  136. break;
  137. case TFDirectionBottom:
  138. {
  139. animationView.frame = self.presentation?finalRect:CGRectOffset(finalRect, 0, finalSize.height);
  140. }
  141. break;
  142. default:
  143. break;
  144. }
  145. alphaView.alpha = self.presentation?0.4f:0;
  146. } completion:^(BOOL finished) {
  147. [transitionContext completeTransition:YES];
  148. }];
  149. }
  150. @end
  151. @interface TFTransitionDelegate : NSObject<UIViewControllerTransitioningDelegate>
  152. @property (nonatomic,assign)TFDirection direction;
  153. -(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
  154. @end
  155. @implementation TFTransitionDelegate
  156. +(instancetype)transtionDelegateWithDirection:(TFDirection)direction
  157. {
  158. TFTransitionDelegate * delegate = [[TFTransitionDelegate alloc] init];
  159. delegate.direction = direction;
  160. return delegate;
  161. }
  162. -(nullable id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
  163. {
  164. return [TFPresentAnimation presentAnimationWithDirection:self.direction isPresentation:YES];
  165. }
  166. -(nullable id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
  167. {
  168. return [TFPresentAnimation presentAnimationWithDirection:self.direction isPresentation:NO];
  169. }
  170. @end
  171. @implementation UIViewController (TFPresent)
  172. - (BOOL)TF_unAllowTouched {
  173. NSNumber *number = objc_getAssociatedObject(self, &unAllowTouched);
  174. return [number boolValue];
  175. }
  176. -(void)TF_setunAllowTouched:(BOOL)disAllowTouch
  177. {
  178. NSNumber *number = [NSNumber numberWithBool: disAllowTouch];
  179. objc_setAssociatedObject(self, &unAllowTouched, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  180. }
  181. -(void)TFPresentVC:(UIViewController *)viewControllerToPresent completion:(PresentCompletion)completion
  182. {
  183. [kAppDelegate.window.rootViewController TF_setunAllowTouched:NO];
  184. viewControllerToPresent.modalPresentationStyle = UIModalPresentationCustom;
  185. viewControllerToPresent.preferredContentSize = CGSizeMake(KScreenWidth, KScreenHeight);
  186. TFTransitionDelegate * transitionDelegate = [TFTransitionDelegate transtionDelegateWithDirection:TFDirectionCenter];
  187. viewControllerToPresent.transitioningDelegate = transitionDelegate;
  188. [[LCTools getNilPresentationController:self] presentViewController:viewControllerToPresent animated:YES completion:completion];
  189. }
  190. -(void)TFPresentViewController:(UIViewController *)viewControllerToPresent presentSize:(CGSize)presentSize direction:(TFDirection)direction completion:(PresentCompletion)completion
  191. {
  192. [kAppDelegate.window.rootViewController TF_setunAllowTouched:NO];
  193. viewControllerToPresent.modalPresentationStyle = UIModalPresentationCustom;
  194. viewControllerToPresent.preferredContentSize = presentSize;
  195. TFTransitionDelegate * transitionDelegate = [TFTransitionDelegate transtionDelegateWithDirection:direction];
  196. viewControllerToPresent.transitioningDelegate = transitionDelegate;
  197. // [self presentViewController:viewControllerToPresent animated:YES completion:completion];
  198. [[LCTools getNilPresentationController:self] presentViewController:viewControllerToPresent animated:YES completion:completion];
  199. }
  200. //add by leo 不能点击返回
  201. -(void)TFPresentViewControllerUnTouched:(UIViewController *)viewControllerToPresent presentSize:(CGSize)presentSize direction:(TFDirection)direction completion:(PresentCompletion)completion
  202. {
  203. //并没有起作用 跳转前和跳转后self不同 add by leo 20191226
  204. // tf_swizzled_method(self.class, @"TFDismissViewController", self.class);
  205. [kAppDelegate.window.rootViewController TF_setunAllowTouched:YES];
  206. viewControllerToPresent.modalPresentationStyle = UIModalPresentationCustom;
  207. viewControllerToPresent.preferredContentSize = presentSize;
  208. TFTransitionDelegate * transitionDelegate = [TFTransitionDelegate transtionDelegateWithDirection:direction];
  209. viewControllerToPresent.transitioningDelegate = transitionDelegate;
  210. [[LCTools getNilPresentationController:self] presentViewController:viewControllerToPresent animated:YES completion:completion];
  211. // //防止二次弹出时位置偏移
  212. // [kAppDelegate.window.rootViewController presentViewController:viewControllerToPresent animated:YES completion:completion];
  213. }
  214. -(void)TFDismissViewController
  215. {
  216. BOOL unalloTouched= [self TF_unAllowTouched];
  217. if(!unalloTouched)
  218. [self dismissViewControllerAnimated:YES completion:nil];
  219. }
  220. -(void)UnTouched_TFDismissViewController
  221. {
  222. // [self UnTouched_TFDismissViewController];
  223. //[self dismissViewControllerAnimated:YES completion:nil];
  224. }
  225. static inline void tf_swizzled_method(Class oldClass ,NSString *oldSelector, Class newClass) {
  226. NSString *newSelector = [NSString stringWithFormat:@"UnTouched_%@", oldSelector];
  227. SEL originalSelector = NSSelectorFromString(oldSelector);
  228. SEL swizzledSelector = NSSelectorFromString(newSelector);
  229. Method originalMethod = class_getInstanceMethod(oldClass, NSSelectorFromString(oldSelector));
  230. Method swizzledMethod = class_getInstanceMethod(newClass, NSSelectorFromString(newSelector));
  231. BOOL isAdd = class_addMethod(oldClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
  232. if (isAdd) {
  233. class_replaceMethod(newClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  234. }else {
  235. method_exchangeImplementations(originalMethod, swizzledMethod);
  236. }
  237. }
  238. @end