FullScreenImageTransitionAnimator.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "FullScreenImageTransitionAnimator.h"
  21. #import "FullscreenImageViewController.h"
  22. #import "GroupDetailsViewController.h"
  23. #import "ContactDetailsViewController.h"
  24. #import "MyIdentityViewController.h"
  25. #import "ContactsViewController.h"
  26. #import "Threema-Swift.h"
  27. @interface FullScreenImageTransitionAnimator ()
  28. @property NSTimeInterval duration;
  29. @end
  30. @implementation FullScreenImageTransitionAnimator
  31. - (instancetype)init
  32. {
  33. self = [super init];
  34. if (self) {
  35. _duration = 0.4f;
  36. }
  37. return self;
  38. }
  39. #pragma mark - UIViewControllerAnimatedTransitioning
  40. - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
  41. return _duration;
  42. }
  43. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  44. {
  45. if ([transitionContext isAnimated] == NO) {
  46. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  47. return;
  48. }
  49. // Setup for animation transition
  50. UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  51. UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  52. if ([fromVC isKindOfClass:[FullscreenImageViewController class]] && [toVC isKindOfClass:[GroupDetailsViewController class]]) {
  53. GroupDetailsViewController *gVC = (GroupDetailsViewController *)toVC;
  54. [self animateImage:(FullscreenImageViewController *)fromVC toImageVC:gVC view:gVC.imageView context:transitionContext];
  55. } else if ([fromVC isKindOfClass:[GroupDetailsViewController class]] && [toVC isKindOfClass:[FullscreenImageViewController class]]) {
  56. GroupDetailsViewController *gVC = (GroupDetailsViewController *)fromVC;
  57. [self animateFromVC:gVC toImageVC:(FullscreenImageViewController *)toVC view:gVC.imageView context:transitionContext];
  58. } else if ([fromVC isKindOfClass:[FullscreenImageViewController class]] && [toVC isKindOfClass:[ContactDetailsViewController class]]) {
  59. ContactDetailsViewController *cVC = (ContactDetailsViewController *)toVC;
  60. [self animateImage:(FullscreenImageViewController *)fromVC toImageVC:cVC view:cVC.imageView context:transitionContext];
  61. } else if ([fromVC isKindOfClass:[ContactDetailsViewController class]] && [toVC isKindOfClass:[FullscreenImageViewController class]]) {
  62. ContactDetailsViewController *cVC = (ContactDetailsViewController *)fromVC;
  63. [self animateFromVC:cVC toImageVC:(FullscreenImageViewController *)toVC view:cVC.imageView context:transitionContext];
  64. } else if ([fromVC isKindOfClass:[FullscreenImageViewController class]] && [toVC isKindOfClass:[MeContactDetailsViewController class]]) {
  65. MeContactDetailsViewController *gVC = (MeContactDetailsViewController *)toVC;
  66. [self animateImage:(FullscreenImageViewController *)fromVC toImageVC:gVC view:gVC.imageView context:transitionContext];
  67. } else if ([fromVC isKindOfClass:[MeContactDetailsViewController class]] && [toVC isKindOfClass:[FullscreenImageViewController class]]) {
  68. MeContactDetailsViewController *gVC = (MeContactDetailsViewController *)fromVC;
  69. [self animateFromVC:gVC toImageVC:(FullscreenImageViewController *)toVC view:gVC.imageView context:transitionContext];
  70. }
  71. else if ([fromVC isKindOfClass:[FullscreenImageViewController class]] && [toVC isKindOfClass:[ContactsViewController class]]) {
  72. ContactsViewController *cVC = (ContactsViewController *)toVC;
  73. [self animateImage:(FullscreenImageViewController *)fromVC toImageVC:cVC view:cVC.segmentedControl context:transitionContext];
  74. }
  75. else {
  76. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  77. }
  78. }
  79. - (void)animateImage:(FullscreenImageViewController *)imageVC toImageVC:(UIViewController *)presentingVC view:(UIView *)view context:(id<UIViewControllerContextTransitioning>)transitionContext {
  80. UIView *containerView = [transitionContext containerView];
  81. UIImageView *animatedView = [imageVC createImageView];
  82. CGRect finalRect = [transitionContext finalFrameForViewController:presentingVC];
  83. CGRect destRect = [presentingVC.view convertRect:view.frame fromView:view.superview];
  84. destRect = CGRectOffset(destRect, 0.0, finalRect.origin.y);
  85. CGRect srcRect = [containerView convertRect:animatedView.frame fromView:imageVC.view];
  86. [containerView addSubview:presentingVC.view];
  87. presentingVC.view.alpha = 0.0;
  88. animatedView.alpha = 1.0;
  89. animatedView.frame = srcRect;
  90. [containerView addSubview:animatedView];
  91. [UIView animateWithDuration:_duration delay:0.0 usingSpringWithDamping:0.75 initialSpringVelocity:0.0 options:0 animations:^{
  92. animatedView.frame = destRect;
  93. animatedView.alpha = 0.0;
  94. presentingVC.view.alpha = 1.0;
  95. } completion:^(BOOL finished) {
  96. [animatedView removeFromSuperview];
  97. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  98. }];
  99. }
  100. - (void)animateFromVC:(UIViewController *)presentingVC toImageVC:(FullscreenImageViewController *)imageVC view:(UIView *)view context:(id<UIViewControllerContextTransitioning>)transitionContext {
  101. UIView *containerView = [transitionContext containerView];
  102. UIImageView *animatedView = [imageVC createImageView];
  103. [containerView addSubview:presentingVC.view];
  104. CGRect finalRect = [transitionContext finalFrameForViewController:imageVC];
  105. animatedView.frame = CGRectOffset(animatedView.frame, 0.0, finalRect.origin.y);
  106. CGRect srcRect = [containerView convertRect:view.frame fromView:view.superview];
  107. CGRect destRect = animatedView.frame;
  108. UIView *bgView = [[UIView alloc] initWithFrame:containerView.bounds];
  109. bgView.backgroundColor = imageVC.view.backgroundColor;
  110. bgView.alpha = 0.0;
  111. [containerView addSubview:bgView];
  112. animatedView.alpha = 0.0;
  113. animatedView.frame = srcRect;
  114. [containerView addSubview:animatedView];
  115. [UIView animateWithDuration:_duration delay:0.0 usingSpringWithDamping:0.75 initialSpringVelocity:0.0 options:0 animations:^{
  116. animatedView.frame = destRect;
  117. animatedView.alpha = 1.0;
  118. bgView.alpha = 1.0;
  119. presentingVC.view.alpha = 0.0;
  120. } completion:^(BOOL finished) {
  121. [containerView addSubview:imageVC.view];
  122. [animatedView removeFromSuperview];
  123. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  124. }];
  125. }
  126. @end