CTAssetsViewControllerTransition.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. MIT License (MIT)
  3. Copyright (c) 2015 Clement CN Tsang
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #import "CTAssetsViewControllerTransition.h"
  21. #import "CTAssetsPickerDefines.h"
  22. #import "CTAssetsGridViewController.h"
  23. #import "CTAssetsPageViewController.h"
  24. #import "CTAssetItemViewController.h"
  25. #import "CTAssetScrollView.h"
  26. #import "CTAssetsPageView.h"
  27. @interface CTAssetsViewControllerTransition ()
  28. @end
  29. @implementation CTAssetsViewControllerTransition
  30. #pragma mark - UIViewControllerAnimatedTransitioning
  31. - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
  32. {
  33. return 0.35f;
  34. }
  35. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  36. {
  37. UIView *containerView = [transitionContext containerView];
  38. UIColor *backgroundColor = [CTAssetsPageView appearance].pageBackgroundColor;
  39. containerView.backgroundColor = (backgroundColor) ? backgroundColor : CTAssetsPageViewPageBackgroundColor;
  40. if (self.operation == UINavigationControllerOperationPush)
  41. {
  42. CTAssetsGridViewController *fromVC = (CTAssetsGridViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  43. CTAssetsPageViewController *toVC = (CTAssetsPageViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  44. CTAssetItemViewController *iVC = (CTAssetItemViewController *)toVC.viewControllers[0];
  45. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:toVC.pageIndex inSection:0];
  46. UIView *cellView = [fromVC.collectionView cellForItemAtIndexPath:indexPath];
  47. UIImageView *imageView = [[UIImageView alloc] initWithImage:iVC.image];
  48. UIView *snapshot = [self resizedSnapshot:imageView];
  49. CGPoint cellCenter = [fromVC.view convertPoint:cellView.center fromView:cellView.superview];
  50. CGPoint snapCenter = toVC.view.center;
  51. // Find the scales of snapshot
  52. float startScale = MAX(cellView.frame.size.width / snapshot.frame.size.width,
  53. cellView.frame.size.height / snapshot.frame.size.height);
  54. float minScale = MIN(toVC.view.frame.size.width / snapshot.frame.size.width,
  55. toVC.view.frame.size.height / snapshot.frame.size.height);
  56. float perspectiveScale = MAX(toVC.view.frame.size.width / snapshot.frame.size.width,
  57. toVC.view.frame.size.height / snapshot.frame.size.height);
  58. BOOL canPerspectiveZoom = ([self canPerspectiveZoomWithImageSize:iVC.image.size boundsSize:toVC.view.bounds.size]);
  59. float endScale = (canPerspectiveZoom) ? perspectiveScale : minScale;
  60. // Find the bounds of the snapshot mask
  61. float width = snapshot.bounds.size.width;
  62. float height = snapshot.bounds.size.height;
  63. float length = MIN(width, height);
  64. CGRect startBounds = CGRectMake((width-length)/2, (height-length)/2, length, length);
  65. // Create the mask
  66. UIView *mask = [[UIView alloc] initWithFrame:startBounds];
  67. mask.backgroundColor = [UIColor whiteColor];
  68. // Prepare transition
  69. snapshot.transform = CGAffineTransformMakeScale(startScale, startScale);;
  70. snapshot.layer.mask = mask.layer;
  71. snapshot.center = cellCenter;
  72. toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
  73. toVC.view.alpha = 0;
  74. // Add to container view
  75. [containerView addSubview:toVC.view];
  76. [containerView addSubview:snapshot];
  77. // Animate
  78. [UIView animateWithDuration:[self transitionDuration:transitionContext]
  79. delay:0
  80. usingSpringWithDamping:0.75
  81. initialSpringVelocity:0
  82. options:UIViewAnimationOptionCurveLinear
  83. animations:^{
  84. fromVC.view.alpha = 0;
  85. snapshot.transform = CGAffineTransformMakeScale(endScale, endScale);
  86. snapshot.layer.mask.bounds = snapshot.bounds;
  87. snapshot.center = snapCenter;
  88. toVC.navigationController.toolbar.alpha = 0;
  89. }
  90. completion:^(BOOL finished){
  91. toVC.view.alpha = 1;
  92. [snapshot removeFromSuperview];
  93. [transitionContext completeTransition:YES];
  94. }];
  95. }
  96. else
  97. {
  98. CTAssetsPageViewController *fromVC = (CTAssetsPageViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  99. CTAssetsGridViewController *toVC = (CTAssetsGridViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  100. CTAssetItemViewController *iVC = (CTAssetItemViewController *)fromVC.viewControllers[0];
  101. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:fromVC.pageIndex inSection:0];
  102. // Scroll to index path
  103. [toVC.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  104. [toVC.collectionView layoutIfNeeded];
  105. UIView *cellView = [toVC.collectionView cellForItemAtIndexPath:indexPath];
  106. CTAssetScrollView *scrollView = iVC.view.subviews[0];
  107. UIImageView *imageView = scrollView.imageView;
  108. UIView *snapshot = [self resizedSnapshot:imageView];
  109. CGPoint cellCenter = [toVC.view convertPoint:cellView.center fromView:cellView.superview];
  110. CGPoint snapCenter = fromVC.view.center;
  111. // Find the scales of snapshot
  112. float startScale = MIN(fromVC.view.frame.size.width / snapshot.frame.size.width,
  113. fromVC.view.frame.size.height / snapshot.frame.size.height);
  114. float endScale = MAX(cellView.frame.size.width / snapshot.frame.size.width,
  115. cellView.frame.size.height / snapshot.frame.size.height);
  116. // Find the bounds of the snapshot mask
  117. float width = snapshot.bounds.size.width;
  118. float height = snapshot.bounds.size.height;
  119. float length = MIN(width, height);
  120. CGRect endBounds = CGRectMake((width-length)/2, (height-length)/2, length, length);
  121. UIView *mask = [[UIView alloc] initWithFrame:snapshot.bounds];
  122. mask.backgroundColor = [UIColor whiteColor];
  123. // Prepare transition
  124. snapshot.transform = CGAffineTransformMakeScale(startScale, startScale);
  125. snapshot.layer.mask = mask.layer;
  126. snapshot.center = snapCenter;
  127. toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
  128. toVC.view.alpha = 0;
  129. fromVC.view.alpha = 0;
  130. // Add to container view
  131. [containerView addSubview:toVC.view];
  132. [containerView addSubview:snapshot];
  133. // Animate
  134. [UIView animateWithDuration:[self transitionDuration:transitionContext]
  135. delay:0
  136. usingSpringWithDamping:1
  137. initialSpringVelocity:0
  138. options:UIViewAnimationOptionCurveLinear
  139. animations:^{
  140. toVC.view.alpha = 1;
  141. snapshot.transform = CGAffineTransformMakeScale(endScale, endScale);
  142. snapshot.layer.mask.bounds = endBounds;
  143. snapshot.center = cellCenter;
  144. toVC.navigationController.toolbar.alpha = 0;
  145. }
  146. completion:^(BOOL finished){
  147. [snapshot removeFromSuperview];
  148. [transitionContext completeTransition:YES];
  149. }];
  150. }
  151. }
  152. #pragma mark - Perspective Zoom
  153. - (BOOL)canPerspectiveZoomWithImageSize:(CGSize)imageSize boundsSize:(CGSize)boundsSize
  154. {
  155. CGFloat imageRatio = imageSize.width / imageSize.height;
  156. CGFloat boundsRatio = boundsSize.width / boundsSize.height;
  157. // can perform perspective zoom when the difference of aspect ratios is smaller than 20%
  158. return (fabs( (imageRatio - boundsRatio) / boundsRatio ) < 0.2f);
  159. }
  160. #pragma mark - Snapshot
  161. - (UIView *)resizedSnapshot:(UIImageView *)imageView
  162. {
  163. CGSize size = imageView.frame.size;
  164. if (CGSizeEqualToSize(size, CGSizeZero))
  165. return nil;
  166. UIGraphicsBeginImageContextWithOptions(size, YES, 0);
  167. [[UIColor whiteColor] set];
  168. UIRectFill(CGRectMake(0, 0, size.width, size.height));
  169. [imageView.image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  170. UIImage *resized = UIGraphicsGetImageFromCurrentImageContext();
  171. UIGraphicsEndImageContext();
  172. return (UIView *)[[UIImageView alloc] initWithImage:resized];
  173. }
  174. @end