RippleAnimationView.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. //RippleAnimationView.m
  3. //boxfish-english
  4. //
  5. //Created by mervin on 2016/11/4.
  6. //Copyright © 2016年 boxfish. All rights reserved.
  7. //
  8. #import "RippleAnimationView.h"
  9. static NSInteger const pulsingCount = 3;
  10. static double const animationDuration = 3;
  11. @interface RippleAnimationView ()
  12. @property (nonatomic, assign) AnimationType animationType;
  13. @end
  14. @implementation RippleAnimationView
  15. - (instancetype)initWithFrame:(CGRect)frame {
  16. return [self initWithFrame:frame animationType:AnimationTypeWithBackground];
  17. }
  18. - (instancetype)initWithFrame:(CGRect)frame animationType:(AnimationType)animationType {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. _animationType = animationType;
  22. self.backgroundColor = [UIColor clearColor];
  23. if (animationType == AnimationTypeWithBackground) {
  24. _multiple = 1.423;
  25. } else {
  26. _multiple = 1.323;
  27. }
  28. [self youpaifsetSubView];
  29. }
  30. return self;
  31. }
  32. -(void)youpaifsetSubView{
  33. CALayer *animationLayer = [CALayer layer];
  34. for (int i = 0; i < pulsingCount; i++) {
  35. NSArray *animationArray = [self animationArray];
  36. CAAnimationGroup *animationGroup = [self animationGroupAnimations:animationArray index:i];
  37. CALayer *pulsingLayer = [self pulsingLayer:self.frame animation:animationGroup];
  38. [animationLayer addSublayer:pulsingLayer];
  39. }
  40. [self.layer addSublayer:animationLayer];
  41. }
  42. - (void)drawRect:(CGRect)rect {
  43. }
  44. - (NSArray *)animationArray {
  45. NSArray *animationArray = nil;
  46. if (_animationType == AnimationTypeWithBackground) {
  47. CABasicAnimation *scaleAnimation = [self scaleAnimation];
  48. CAKeyframeAnimation *borderColorAnimation = [self borderColorAnimation];
  49. CAKeyframeAnimation *backgroundColorAnimation = [self backgroundColorAnimation];
  50. animationArray = @[scaleAnimation, backgroundColorAnimation, borderColorAnimation];
  51. } else {
  52. CABasicAnimation *scaleAnimation = [self scaleAnimation];
  53. CAKeyframeAnimation *blackBorderColorAnimation = [self blackBorderColorAnimation];
  54. animationArray = @[scaleAnimation, blackBorderColorAnimation];
  55. }
  56. return animationArray;
  57. }
  58. - (CAAnimationGroup *)animationGroupAnimations:(NSArray *)array index:(int)index {
  59. CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
  60. CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
  61. animationGroup.fillMode = kCAFillModeBackwards;
  62. animationGroup.beginTime = CACurrentMediaTime() + (double)(index * animationDuration) / (double)pulsingCount;
  63. animationGroup.duration = animationDuration;
  64. animationGroup.repeatCount = HUGE;
  65. animationGroup.timingFunction = defaultCurve;
  66. animationGroup.animations = array;
  67. animationGroup.removedOnCompletion = NO;
  68. return animationGroup;
  69. }
  70. - (CALayer *)pulsingLayer:(CGRect)rect animation:(CAAnimationGroup *)animationGroup {
  71. CALayer *pulsingLayer = [CALayer layer];
  72. pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
  73. if (_animationType == AnimationTypeWithBackground) {
  74. pulsingLayer.backgroundColor = ColorWithAlpha(255, 216, 87, 0.5).CGColor;
  75. pulsingLayer.borderWidth = 0.5;
  76. } else {
  77. pulsingLayer.borderWidth = 2.f;
  78. }
  79. // pulsingLayer.borderColor = ColorWithAlpha(255, 0, 132, 0.5).CGColor;
  80. pulsingLayer.cornerRadius = rect.size.height / 2;
  81. [pulsingLayer addAnimation:animationGroup forKey:@"plulsing"];
  82. return pulsingLayer;
  83. }
  84. - (CABasicAnimation *)scaleAnimation {
  85. CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  86. scaleAnimation.fromValue = @1;
  87. scaleAnimation.toValue = @(_multiple);
  88. return scaleAnimation;
  89. }
  90. - (CAKeyframeAnimation *)backgroundColorAnimation {
  91. CAKeyframeAnimation *backgroundColorAnimation = [CAKeyframeAnimation animation];
  92. backgroundColorAnimation.keyPath = @"backgroundColor";
  93. backgroundColorAnimation.values = @[(__bridge id)ColorWithAlpha(255, 0, 13, 0.5).CGColor,
  94. (__bridge id)ColorWithAlpha(255, 0, 13, 0.5).CGColor,
  95. (__bridge id)ColorWithAlpha(255, 58, 0, 0.5).CGColor,
  96. (__bridge id)ColorWithAlpha(255, 58, 0, 0).CGColor];
  97. backgroundColorAnimation.keyTimes = @[@0.3,@0.6,@0.9,@1];
  98. return backgroundColorAnimation;
  99. }
  100. - (CAKeyframeAnimation *)borderColorAnimation {
  101. CAKeyframeAnimation *borderColorAnimation = [CAKeyframeAnimation animation];
  102. borderColorAnimation.keyPath = @"borderColor";
  103. borderColorAnimation.values = @[(__bridge id)ColorWithAlpha(255, 216, 87, 0.5).CGColor,
  104. (__bridge id)ColorWithAlpha(255, 231, 152, 0.5).CGColor,
  105. (__bridge id)ColorWithAlpha(255, 241, 197, 0.5).CGColor,
  106. (__bridge id)ColorWithAlpha(255, 241, 197, 0).CGColor];
  107. borderColorAnimation.keyTimes = @[@0.3,@0.6,@0.9,@1];
  108. return borderColorAnimation;
  109. }
  110. - (CAKeyframeAnimation *)blackBorderColorAnimation {
  111. CAKeyframeAnimation *borderColorAnimation = [CAKeyframeAnimation animation];
  112. // ic_live_wai_quan
  113. // vqu_images_L_live_nei_quan
  114. borderColorAnimation.keyPath = @"borderColor";
  115. borderColorAnimation.values = @[(__bridge id)ColorWithAlpha(255, 0, 132, 0.7).CGColor,
  116. (__bridge id)ColorWithAlpha(255, 58, 0, 0.45).CGColor,
  117. (__bridge id)ColorWithAlpha(255, 0, 132, 0.2).CGColor,
  118. // (__bridge id)ColorWithAlpha(255, 0, 132, 0.0).CGColor
  119. ];
  120. borderColorAnimation.keyTimes = @[@0.3,@0.6,@0.5,@1];
  121. return borderColorAnimation;
  122. }
  123. @end