CustomModalViewTransition.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // CustomModalViewTransition.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/23.
  6. //
  7. #import "PCSBaseViewController.h"
  8. #import "CustomModalViewTransition.h"
  9. #import "PhotoListViewController.h"
  10. #import "PhotoCollectionViewCell.h"
  11. #import "PhotoListCellModel.h"
  12. #import "LenzHeader.h"
  13. @implementation CustomModalViewTransitionPresent
  14. - (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
  15. PCSBaseViewController* fromVC = (PCSBaseViewController*)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  16. PhotoListViewController* toVC = (PhotoListViewController*)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  17. UIView* container = transitionContext.containerView;
  18. id<OperationNodeProtocol> curr = fromVC.currentOperationNode;
  19. // UIView* snapshotView = [fromVC.albumButton.imageView snapshotViewAfterScreenUpdates:NO];
  20. UIImage* img = nil;
  21. if([curr respondsToSelector:@selector(firstCoverImage)]){
  22. img = curr.firstCoverImage;
  23. }
  24. UIImageView* snapshotView = [[UIImageView alloc] initWithImage:img];
  25. snapshotView.contentMode = UIViewContentModeScaleAspectFit;
  26. snapshotView.frame = [fromVC.albumButton convertRect:fromVC.albumButton.imageView.frame toView:container];
  27. fromVC.albumButton.imageView.hidden = YES;
  28. toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
  29. toVC.view.alpha = 0;
  30. [container addSubview:toVC.view];
  31. [container addSubview:snapshotView];
  32. container.backgroundColor = toVC.view.backgroundColor;
  33. [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseIn animations:^{
  34. fromVC.view.alpha = 0;
  35. snapshotView.frame = CGRectMake(15, 15, CGRectGetWidth(toVC.photoCollectionView.frame) - 30, CGRectGetHeight(toVC.photoCollectionView.frame) - 30);
  36. } completion:^(BOOL finished) {
  37. fromVC.albumButton.imageView.hidden = NO;
  38. [snapshotView removeFromSuperview];
  39. toVC.view.alpha = 1;
  40. [transitionContext completeTransition:YES];
  41. }];
  42. }
  43. - (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
  44. return 0.5;
  45. }
  46. @end
  47. @implementation CustomModalViewTransitionDismiss
  48. - (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
  49. PhotoListViewController* fromVC = (PhotoListViewController*)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  50. PCSBaseViewController* toVC = (PCSBaseViewController*)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  51. UIView* container = transitionContext.containerView;
  52. NSArray<__kindof UICollectionViewCell *> *visibleCells = fromVC.photoCollectionView.visibleCells;
  53. __block PhotoCollectionViewCell* currCell = nil;
  54. [visibleCells enumerateObjectsUsingBlock:^(__kindof UICollectionViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  55. if(CGRectContainsRect(fromVC.photoCollectionView.bounds, obj.frame)){
  56. *stop = YES;
  57. currCell = obj;
  58. }
  59. }];
  60. UIView* snapshotView = [currCell.coverView snapshotViewAfterScreenUpdates:NO];
  61. snapshotView.frame = [fromVC.photoCollectionView convertRect:currCell.frame toView:container];
  62. currCell.imageView.hidden = YES;
  63. toVC.albumButton.imageView.hidden = YES;
  64. toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
  65. toVC.view.alpha = 0;
  66. [container addSubview:toVC.view];
  67. [container addSubview:snapshotView];
  68. [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseIn animations:^{
  69. snapshotView.frame = toVC.albumButton.frame;
  70. toVC.view.alpha = 1;
  71. } completion:^(BOOL finished) {
  72. toVC.albumButton.imageView.hidden = NO;
  73. [snapshotView removeFromSuperview];
  74. [transitionContext completeTransition:YES];
  75. }];
  76. }
  77. - (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
  78. return 0.5;
  79. }
  80. @end