RAActivityIndicator.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // RAActivityIndicator.m
  3. // Test
  4. //
  5. // Created by Jack on 2019/2/1.
  6. // Copyright © 2019 Jack Template. All rights reserved.
  7. //
  8. #import "RAActivityIndicator.h"
  9. static const NSInteger kReplicationCount = 3;
  10. static const CGFloat kSize = 20.f;
  11. static const CGFloat kInterval = 5.f;
  12. static const CGFloat kWidth = kSize * kReplicationCount + kInterval * (kReplicationCount - 1);
  13. static const CGFloat kHeight = kSize;
  14. static const NSTimeInterval kAnimateDuration = 1.f;
  15. static NSString * const kAnimationKey = @"ra.activity.indictor.animation";
  16. @interface RAActivityIndicator ()
  17. @property (nonatomic,strong) CAReplicatorLayer *replicatorLayer;
  18. @property (nonatomic,strong) CAShapeLayer *activityLayer;
  19. @end
  20. @implementation RAActivityIndicator
  21. + (instancetype)activityIndicatorWithType:(RAActivityType)type {
  22. RAActivityIndicator *loadingView = [[RAActivityIndicator alloc] initPrivateFrame:CGRectMake(0, 0, kWidth, kHeight)];
  23. [loadingView setup:type];
  24. return loadingView;
  25. }
  26. #pragma mark - Override
  27. - (instancetype)initWithFrame:(CGRect)frame {
  28. if (self = [super initWithFrame:CGRectZero]) {
  29. }
  30. return self;
  31. }
  32. - (void)setFrame:(CGRect)frame {
  33. }
  34. - (void)setBounds:(CGRect)bounds {
  35. }
  36. - (void)setTintColor:(UIColor *)tintColor {
  37. [super setTintColor:tintColor];
  38. self.activityLayer.fillColor = tintColor.CGColor;
  39. }
  40. - (void)setHidden:(BOOL)hidden {
  41. [super setHidden:hidden];
  42. if (hidden) {
  43. [self stopAnimating];
  44. } else{
  45. [self startAnimating];
  46. }
  47. }
  48. #pragma mark - Setup
  49. - (void)ra_setFrame:(CGRect)frame {
  50. super.frame = frame;
  51. }
  52. - (instancetype)initPrivateFrame:(CGRect)frame {
  53. if (self = [super initWithFrame:frame]) {
  54. [self ra_setFrame:frame];
  55. }
  56. return self;
  57. }
  58. - (void)setup:(RAActivityType)type {
  59. self.replicatorLayer.frame = self.bounds;
  60. [self.layer addSublayer:self.replicatorLayer];
  61. switch (type) {
  62. case RAActivityTypeDot: {
  63. self.activityLayer.path = [self dotPath].CGPath;
  64. }
  65. break;
  66. case RAActivityTypeSlash: {
  67. self.activityLayer.path = [self slashPath].CGPath;
  68. }
  69. break;
  70. }
  71. [self.replicatorLayer addSublayer:self.activityLayer];
  72. [self startAnimating];
  73. }
  74. #pragma mark - Path
  75. // 斜杠
  76. - (UIBezierPath *)slashPath {
  77. UIBezierPath *path = [UIBezierPath bezierPath];
  78. [path moveToPoint:CGPointMake(0, 0)];
  79. [path addLineToPoint:CGPointMake(kSize * 0.5, 0)];
  80. [path addLineToPoint:CGPointMake(kSize, kSize)];
  81. [path addLineToPoint:CGPointMake(kSize * 0.5, kSize)];
  82. [path closePath];
  83. return path;
  84. }
  85. // 圆点
  86. - (UIBezierPath *)dotPath {
  87. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, kSize, kSize)];
  88. return path;
  89. }
  90. #pragma mark - Getter
  91. - (CAReplicatorLayer *)replicatorLayer {
  92. if (!_replicatorLayer) {
  93. _replicatorLayer = [CAReplicatorLayer layer];
  94. _replicatorLayer.backgroundColor = [UIColor clearColor].CGColor;
  95. }
  96. return _replicatorLayer;
  97. }
  98. - (CAShapeLayer *)activityLayer {
  99. if (!_activityLayer) {
  100. _activityLayer = [CAShapeLayer layer];
  101. _activityLayer.fillColor = [UIColor darkGrayColor].CGColor;
  102. }
  103. return _activityLayer;
  104. }
  105. #pragma mark - Animation
  106. - (CABasicAnimation *)alphaAnimation{
  107. //设置透明度动画
  108. CABasicAnimation *alpha = [CABasicAnimation animationWithKeyPath:@"opacity"];
  109. alpha.fromValue = @1.0;
  110. alpha.toValue = @0.01;
  111. alpha.duration = 1.f;
  112. return alpha;
  113. }
  114. - (CABasicAnimation *)scaleAnimation{
  115. //设置缩放动画
  116. CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  117. scale.toValue = @0.8;
  118. scale.fromValue = @1;
  119. return scale;
  120. }
  121. - (CABasicAnimation *)positionAnimation {
  122. CABasicAnimation *center = [CABasicAnimation animationWithKeyPath:@"position"];
  123. center.fromValue = [NSValue valueWithCGPoint:CGPointMake(kSize * 0.5f, 0)];
  124. center.toValue = [NSValue valueWithCGPoint:CGPointMake(kWidth - kSize * 0.5f, 0)];
  125. return center;
  126. }
  127. - (void)startAnimating {
  128. if (![self.activityLayer.animationKeys containsObject:kAnimationKey] && !self.hidden) {
  129. //复制的图层数为三个
  130. self.replicatorLayer.instanceCount = kReplicationCount;
  131. //设置每个复制图层延迟时间
  132. self.replicatorLayer.instanceDelay = kAnimateDuration / kReplicationCount;
  133. //设置每个图层之间的偏移
  134. self.replicatorLayer.instanceTransform = CATransform3DMakeTranslation(kSize + kInterval, 0, 0);
  135. CAAnimationGroup *group = [CAAnimationGroup animation];
  136. group.animations = @[[self alphaAnimation], [self scaleAnimation]];
  137. group.duration = kAnimateDuration;
  138. group.repeatCount = HUGE;
  139. [self.activityLayer addAnimation:group forKey:kAnimationKey];
  140. }
  141. }
  142. - (void)stopAnimating {
  143. if ([self.activityLayer.animationKeys containsObject:kAnimationKey]) {
  144. //复制的图层数为三个
  145. self.replicatorLayer.instanceCount = 0;
  146. [self.activityLayer removeAnimationForKey:kAnimationKey];
  147. }
  148. }
  149. @end