NavigationAnimator.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // NavigationAnimator.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/29.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #import "NavigationAnimator.h"
  9. #import "BaseViewController.h"
  10. @interface NavigationAnimator ()
  11. @property UINavigationControllerOperation operation;
  12. @property UILabel* titleView;
  13. @end
  14. @implementation NavigationAnimator
  15. - (instancetype)initWithOperation:(UINavigationControllerOperation)operation titleView:(UILabel*)titleView {
  16. self = [super init];
  17. self.operation = operation;
  18. self.titleView = titleView;
  19. return self;
  20. }
  21. - (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
  22. UIView* fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
  23. UIView* toView = [transitionContext viewForKey:UITransitionContextToViewKey];
  24. if (self.titleView != nil) {
  25. UIViewController* viewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  26. if ([viewController isKindOfClass:BaseViewController.self]) {
  27. self.titleView.text = [(BaseViewController*)viewController topTitle];
  28. }
  29. }
  30. switch (self.operation) {
  31. case UINavigationControllerOperationPush: {
  32. toView.frame = fromView.frame;
  33. toView.transform = CGAffineTransformMakeTranslation(toView.frame.size.width, 0);
  34. fromView.transform = CGAffineTransformIdentity;
  35. [transitionContext.containerView addSubview:toView];
  36. [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:100 initialSpringVelocity:0 options:0 animations:^{
  37. fromView.transform = CGAffineTransformMakeTranslation(-toView.frame.size.width, 0);
  38. toView.transform = CGAffineTransformIdentity;
  39. } completion:^(BOOL finished) {
  40. fromView.transform = CGAffineTransformIdentity;
  41. [fromView removeFromSuperview];
  42. [transitionContext completeTransition:YES];
  43. }];
  44. break;
  45. }
  46. case UINavigationControllerOperationPop: {
  47. toView.frame = fromView.frame;
  48. toView.transform = CGAffineTransformMakeTranslation(-toView.frame.size.width, 0);
  49. fromView.transform = CGAffineTransformIdentity;
  50. [transitionContext.containerView addSubview:toView];
  51. [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:100 initialSpringVelocity:0 options:0 animations:^{
  52. fromView.transform = CGAffineTransformMakeTranslation(toView.frame.size.width, 0);
  53. toView.transform = CGAffineTransformIdentity;
  54. } completion:^(BOOL finished) {
  55. fromView.transform = CGAffineTransformIdentity;
  56. [fromView removeFromSuperview];
  57. [transitionContext completeTransition:YES];
  58. }];
  59. break;
  60. }
  61. default:
  62. break;
  63. }
  64. }
  65. - (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
  66. return 0.25;
  67. }
  68. @end