猎豆优选

ZLInteractiveTrasition.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // ZLInteractiveTrasition.m
  3. // ZLPhotoBrowser
  4. //
  5. // Created by long on 2018/8/12.
  6. // Copyright © 2018年 long. All rights reserved.
  7. //
  8. #import "ZLInteractiveTrasition.h"
  9. #import "ZLShowBigImgViewController.h"
  10. #import "ZLInteractiveAnimateProtocol.h"
  11. @interface ZLInteractiveTrasition ()
  12. @property (nonatomic, assign, readwrite) BOOL isStartTransition;
  13. @property (nonatomic, strong) UIView *shadowView;
  14. @property (nonatomic, weak) id<UIViewControllerContextTransitioning> transitionContext;
  15. @end
  16. @implementation ZLInteractiveTrasition
  17. - (void)startInteractiveTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  18. {
  19. self.isStartTransition = YES;
  20. self.transitionContext = transitionContext;
  21. [self beginAnimate];
  22. }
  23. - (void)beginAnimate
  24. {
  25. UIView *fromView = [self.transitionContext viewForKey:UITransitionContextFromViewKey];
  26. UIView *toView = [self.transitionContext viewForKey:UITransitionContextToViewKey];
  27. UIViewController *fromVC = [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  28. ZLShowBigImgViewController *imageVC = (ZLShowBigImgViewController *)fromVC;
  29. imageVC->_navView.hidden = YES;
  30. NSInteger index = imageVC->_currentPage-1;
  31. UIViewController *toVC = [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  32. if ([toVC conformsToProtocol:@protocol(ZLInteractiveAnimateProtocol)]) {
  33. if ([(id<ZLInteractiveAnimateProtocol>)toVC respondsToSelector:@selector(scrollToIndex:)]) {
  34. [(id<ZLInteractiveAnimateProtocol>)toVC scrollToIndex:index];
  35. }
  36. }
  37. UIView *containerView = self.transitionContext.containerView;
  38. self.shadowView = [[UIView alloc] init];
  39. self.shadowView.backgroundColor = [UIColor blackColor];
  40. self.shadowView.frame = toView.bounds;
  41. [containerView addSubview:toView];
  42. [containerView addSubview:self.shadowView];
  43. [containerView addSubview:fromView];
  44. }
  45. - (void)updatePercent:(CGFloat)percent
  46. {
  47. UIView *fromView = [self.transitionContext viewForKey:UITransitionContextFromViewKey];
  48. CGRect frame = CGRectMake(0, CGRectGetHeight(fromView.bounds)*percent, CGRectGetWidth(fromView.bounds), CGRectGetHeight(fromView.bounds));
  49. fromView.frame = frame;
  50. self.shadowView.alpha = MIN(1, MAX(0, 1-percent));
  51. }
  52. - (void)finishAnimate
  53. {
  54. self.isStartTransition = NO;
  55. UIView *fromView = [self.transitionContext viewForKey:UITransitionContextFromViewKey];
  56. CGRect frame = fromView.frame;
  57. frame.origin.y = frame.size.height;
  58. [UIView animateWithDuration:0.25 animations:^{
  59. fromView.frame = frame;
  60. self.shadowView.alpha = 0;
  61. } completion:^(BOOL finished) {
  62. [self.shadowView removeFromSuperview];
  63. [self.transitionContext completeTransition:!self.transitionContext.transitionWasCancelled];
  64. }];
  65. }
  66. - (void)cancelAnimate
  67. {
  68. self.isStartTransition = NO;
  69. UIViewController *fromVC = [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  70. ZLShowBigImgViewController *imageVC = (ZLShowBigImgViewController *)fromVC;
  71. UIView *fromView = [self.transitionContext viewForKey:UITransitionContextFromViewKey];
  72. CGRect frame = fromView.frame;
  73. frame.origin.y = 0;
  74. [UIView animateWithDuration:0.25 animations:^{
  75. fromView.frame = frame;
  76. self.shadowView.alpha = 1;
  77. } completion:^(BOOL finished) {
  78. imageVC->_navView.hidden = NO;
  79. [self.shadowView removeFromSuperview];
  80. [self.transitionContext completeTransition:!self.transitionContext.transitionWasCancelled];
  81. }];
  82. }
  83. @end