YBPopupMenuAnimationManager.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // YBPopupMenuAnimationManager.m
  3. // YBPopupMenuDemo
  4. //
  5. // Created by liyuanbo on 2020/1/19.
  6. // Copyright © 2020 LYB. All rights reserved.
  7. //
  8. #import "YBPopupMenuAnimationManager.h"
  9. static NSString * const YBShowAnimationKey = @"showAnimation";
  10. static NSString * const YBDismissAnimationKey = @"dismissAnimation";
  11. @interface YBPopupMenuAnimationManager ()
  12. <
  13. CAAnimationDelegate
  14. >
  15. @property (nonatomic, copy) void (^showAnimationHandle) (void);
  16. @property (nonatomic, copy) void (^dismissAnimationHandle) (void);
  17. @property (nonatomic, assign) BOOL isAnimating;
  18. @end
  19. @implementation YBPopupMenuAnimationManager
  20. @synthesize style = _style;
  21. @synthesize showAnimation = _showAnimation;
  22. @synthesize dismissAnimation = _dismissAnimation;
  23. @synthesize duration = _duration;
  24. @synthesize animationView = _animationView;
  25. + (id<YBPopupMenuAnimationManager>)manager
  26. {
  27. YBPopupMenuAnimationManager * manager = [[YBPopupMenuAnimationManager alloc] init];
  28. manager.style = YBPopupMenuAnimationStyleScale;
  29. manager.duration = 0.25;
  30. return manager;
  31. }
  32. - (void)configAnimation
  33. {
  34. CABasicAnimation * showAnimation;
  35. CABasicAnimation * dismissAnimation;
  36. switch (_style) {
  37. case YBPopupMenuAnimationStyleFade:
  38. {
  39. _showAnimation = _dismissAnimation = nil;
  40. //show
  41. showAnimation = [self getBasicAnimationWithKeyPath:@"opacity"];
  42. showAnimation.fillMode = kCAFillModeBackwards;
  43. showAnimation.fromValue = @(0);
  44. showAnimation.toValue = @(1);
  45. _showAnimation = showAnimation;
  46. //dismiss
  47. dismissAnimation = [self getBasicAnimationWithKeyPath:@"opacity"];
  48. dismissAnimation.fillMode = kCAFillModeForwards;
  49. dismissAnimation.fromValue = @(1);
  50. dismissAnimation.toValue = @(0);
  51. _dismissAnimation = dismissAnimation;
  52. }
  53. break;
  54. case YBPopupMenuAnimationStyleCustom:
  55. break;
  56. case YBPopupMenuAnimationStyleNone:
  57. {
  58. _showAnimation = _dismissAnimation = nil;
  59. }
  60. break;
  61. default:
  62. {
  63. _showAnimation = _dismissAnimation = nil;
  64. //show
  65. showAnimation = [self getBasicAnimationWithKeyPath:@"transform.scale"];
  66. showAnimation.fillMode = kCAFillModeBackwards;
  67. showAnimation.fromValue = @(0.1);
  68. showAnimation.toValue = @(1);
  69. _showAnimation = showAnimation;
  70. //dismiss
  71. dismissAnimation = [self getBasicAnimationWithKeyPath:@"transform.scale"];
  72. dismissAnimation.fillMode = kCAFillModeForwards;
  73. dismissAnimation.fromValue = @(1);
  74. dismissAnimation.toValue = @(0.1);
  75. _dismissAnimation = dismissAnimation;
  76. }
  77. break;
  78. }
  79. }
  80. - (void)setStyle:(YBPopupMenuAnimationStyle)style
  81. {
  82. _style = style;
  83. [self configAnimation];
  84. }
  85. - (void)setDuration:(CFTimeInterval)duration
  86. {
  87. _duration = duration;
  88. [self configAnimation];
  89. }
  90. - (void)setShowAnimation:(CAAnimation *)showAnimation
  91. {
  92. _showAnimation = showAnimation;
  93. [self configAnimation];
  94. }
  95. - (void)setDismissAnimation:(CAAnimation *)dismissAnimation
  96. {
  97. _dismissAnimation = dismissAnimation;
  98. [self configAnimation];
  99. }
  100. - (CABasicAnimation *)getBasicAnimationWithKeyPath:(NSString *)keyPath
  101. {
  102. CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:keyPath];
  103. animation.removedOnCompletion = NO;
  104. animation.duration = _duration;
  105. return animation;
  106. }
  107. - (void)displayShowAnimationCompletion:(void (^)(void))completion
  108. {
  109. _showAnimationHandle = completion;
  110. if (!_showAnimation) {
  111. if (_showAnimationHandle) {
  112. _showAnimationHandle();
  113. }
  114. return;
  115. }
  116. if (self.isAnimating) return;
  117. self.isAnimating = YES;
  118. _showAnimation.delegate = self;
  119. [_animationView.layer addAnimation:_showAnimation forKey:YBShowAnimationKey];
  120. }
  121. - (void)displayDismissAnimationCompletion:(void (^)(void))completion
  122. {
  123. _dismissAnimationHandle = completion;
  124. if (!_dismissAnimation) {
  125. if (_dismissAnimationHandle) {
  126. _dismissAnimationHandle();
  127. }
  128. return;
  129. }
  130. if (self.isAnimating) return;
  131. self.isAnimating = YES;
  132. _dismissAnimation.delegate = self;
  133. [_animationView.layer addAnimation:_dismissAnimation forKey:YBDismissAnimationKey];
  134. }
  135. #pragma mark - CAAnimationDelegate
  136. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
  137. {
  138. if ([_animationView.layer animationForKey:YBShowAnimationKey] == anim) {
  139. [_animationView.layer removeAnimationForKey:YBShowAnimationKey];
  140. _showAnimation.delegate = nil;
  141. _showAnimation = nil;
  142. _isAnimating = NO;
  143. if (_showAnimationHandle) {
  144. _showAnimationHandle();
  145. }
  146. }else if ([_animationView.layer animationForKey:YBDismissAnimationKey] == anim) {
  147. [_animationView.layer removeAnimationForKey:YBDismissAnimationKey];
  148. _dismissAnimation.delegate = nil;
  149. _dismissAnimation = nil;
  150. _isAnimating = NO;
  151. if (_dismissAnimationHandle) {
  152. _dismissAnimationHandle();
  153. }
  154. }
  155. }
  156. @end