ZFLoadingView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // ZFLoadingView.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFLoadingView.h"
  25. @interface ZFLoadingView ()
  26. @property (nonatomic, strong, readonly) CAShapeLayer *shapeLayer;
  27. @property (nonatomic, assign, getter=isAnimating) BOOL animating;
  28. @property (nonatomic, assign) BOOL strokeShow;
  29. @end
  30. @implementation ZFLoadingView
  31. @synthesize lineColor = _lineColor;
  32. @synthesize shapeLayer = _shapeLayer;
  33. - (instancetype)initWithFrame:(CGRect)frame {
  34. self = [super initWithFrame:frame];
  35. if (self) {
  36. [self initialize];
  37. }
  38. return self;
  39. }
  40. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  41. if (self = [super initWithCoder:aDecoder]) {
  42. [self initialize];
  43. }
  44. return self;
  45. }
  46. - (void)awakeFromNib {
  47. [super awakeFromNib];
  48. [self initialize];
  49. }
  50. - (void)initialize {
  51. [self.layer addSublayer:self.shapeLayer];
  52. self.duration = 1;
  53. self.lineWidth = 1;
  54. self.lineColor = [UIColor whiteColor];
  55. self.userInteractionEnabled = NO;
  56. }
  57. - (void)layoutSubviews {
  58. [super layoutSubviews];
  59. CGFloat width = MIN(self.bounds.size.width, self.bounds.size.height);
  60. CGFloat height = width;
  61. self.shapeLayer.frame = CGRectMake(0, 0, width, height);
  62. CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
  63. CGFloat radius = MIN(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2) - self.shapeLayer.lineWidth / 2;
  64. CGFloat startAngle = (CGFloat)(0);
  65. CGFloat endAngle = (CGFloat)(2*M_PI);
  66. UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
  67. self.shapeLayer.path = path.CGPath;
  68. }
  69. - (void)startAnimating {
  70. if (self.animating) return;
  71. self.animating = YES;
  72. if (self.animType == ZFLoadingTypeFadeOut) [self fadeOutShow];
  73. CABasicAnimation *rotationAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  74. rotationAnim.toValue = [NSNumber numberWithFloat:2 * M_PI];
  75. rotationAnim.duration = self.duration;
  76. rotationAnim.repeatCount = CGFLOAT_MAX;
  77. rotationAnim.removedOnCompletion = NO;
  78. [self.shapeLayer addAnimation:rotationAnim forKey:@"rotation"];
  79. if (self.hidesWhenStopped) {
  80. self.hidden = NO;
  81. }
  82. }
  83. - (void)stopAnimating {
  84. if (!self.animating) return;
  85. self.animating = NO;
  86. [self.shapeLayer removeAllAnimations];
  87. if (self.hidesWhenStopped) {
  88. self.hidden = YES;
  89. }
  90. }
  91. - (void)fadeOutShow {
  92. CABasicAnimation *headAnimation = [CABasicAnimation animation];
  93. headAnimation.keyPath = @"strokeStart";
  94. headAnimation.duration = self.duration / 1.5f;
  95. headAnimation.fromValue = @(0.f);
  96. headAnimation.toValue = @(0.25f);
  97. CABasicAnimation *tailAnimation = [CABasicAnimation animation];
  98. tailAnimation.keyPath = @"strokeEnd";
  99. tailAnimation.duration = self.duration / 1.5f;
  100. tailAnimation.fromValue = @(0.f);
  101. tailAnimation.toValue = @(1.f);
  102. CABasicAnimation *endHeadAnimation = [CABasicAnimation animation];
  103. endHeadAnimation.keyPath = @"strokeStart";
  104. endHeadAnimation.beginTime = self.duration / 1.5f;
  105. endHeadAnimation.duration = self.duration / 3.0f;
  106. endHeadAnimation.fromValue = @(0.25f);
  107. endHeadAnimation.toValue = @(1.f);
  108. CABasicAnimation *endTailAnimation = [CABasicAnimation animation];
  109. endTailAnimation.keyPath = @"strokeEnd";
  110. endTailAnimation.beginTime = self.duration / 1.5f;
  111. endTailAnimation.duration = self.duration / 3.0f;
  112. endTailAnimation.fromValue = @(1.f);
  113. endTailAnimation.toValue = @(1.f);
  114. CAAnimationGroup *animations = [CAAnimationGroup animation];
  115. [animations setDuration:self.duration];
  116. [animations setAnimations:@[headAnimation, tailAnimation, endHeadAnimation, endTailAnimation]];
  117. animations.repeatCount = INFINITY;
  118. animations.removedOnCompletion = NO;
  119. [self.shapeLayer addAnimation:animations forKey:@"strokeAnim"];
  120. if (self.hidesWhenStopped) {
  121. self.hidden = NO;
  122. }
  123. }
  124. #pragma mark - setter and getter
  125. - (CAShapeLayer *)shapeLayer {
  126. if (!_shapeLayer) {
  127. _shapeLayer = [CAShapeLayer layer];
  128. _shapeLayer.strokeColor = self.lineColor.CGColor;
  129. _shapeLayer.fillColor = [UIColor clearColor].CGColor;
  130. _shapeLayer.strokeStart = 0.1;
  131. _shapeLayer.strokeEnd = 1;
  132. _shapeLayer.lineCap = @"round";
  133. _shapeLayer.anchorPoint = CGPointMake(0.5, 0.5);
  134. }
  135. return _shapeLayer;
  136. }
  137. - (UIColor *)lineColor {
  138. if (!_lineColor) {
  139. return [UIColor whiteColor];
  140. }
  141. return _lineColor;
  142. }
  143. - (void)setLineWidth:(CGFloat)lineWidth {
  144. _lineWidth = lineWidth;
  145. self.shapeLayer.lineWidth = lineWidth;
  146. }
  147. - (void)setLineColor:(UIColor *)lineColor {
  148. if (!lineColor) return;
  149. _lineColor = lineColor;
  150. self.shapeLayer.strokeColor = lineColor.CGColor;
  151. }
  152. - (void)setHidesWhenStopped:(BOOL)hidesWhenStopped {
  153. _hidesWhenStopped = hidesWhenStopped;
  154. self.hidden = !self.isAnimating && hidesWhenStopped;
  155. }
  156. @end