SVProgressAnimatedView.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // SVProgressAnimatedView.m
  3. // SVProgressHUD, https://github.com/SVProgressHUD/SVProgressHUD
  4. //
  5. // Copyright (c) 2017-2018 Tobias Tiemerding. All rights reserved.
  6. //
  7. #import "SVProgressAnimatedView.h"
  8. @interface SVProgressAnimatedView ()
  9. @property (nonatomic, strong) CAShapeLayer *ringAnimatedLayer;
  10. @end
  11. @implementation SVProgressAnimatedView
  12. - (void)willMoveToSuperview:(UIView*)newSuperview {
  13. if (newSuperview) {
  14. [self layoutAnimatedLayer];
  15. } else {
  16. [_ringAnimatedLayer removeFromSuperlayer];
  17. _ringAnimatedLayer = nil;
  18. }
  19. }
  20. - (void)layoutAnimatedLayer {
  21. CALayer *layer = self.ringAnimatedLayer;
  22. [self.layer addSublayer:layer];
  23. CGFloat widthDiff = CGRectGetWidth(self.bounds) - CGRectGetWidth(layer.bounds);
  24. CGFloat heightDiff = CGRectGetHeight(self.bounds) - CGRectGetHeight(layer.bounds);
  25. layer.position = CGPointMake(CGRectGetWidth(self.bounds) - CGRectGetWidth(layer.bounds) / 2 - widthDiff / 2, CGRectGetHeight(self.bounds) - CGRectGetHeight(layer.bounds) / 2 - heightDiff / 2);
  26. }
  27. - (CAShapeLayer*)ringAnimatedLayer {
  28. if(!_ringAnimatedLayer) {
  29. CGPoint arcCenter = CGPointMake(self.radius+self.strokeThickness/2+5, self.radius+self.strokeThickness/2+5);
  30. UIBezierPath* smoothedPath = [UIBezierPath bezierPathWithArcCenter:arcCenter radius:self.radius startAngle:(CGFloat)-M_PI_2 endAngle:(CGFloat) (M_PI + M_PI_2) clockwise:YES];
  31. _ringAnimatedLayer = [CAShapeLayer layer];
  32. _ringAnimatedLayer.contentsScale = [[UIScreen mainScreen] scale];
  33. _ringAnimatedLayer.frame = CGRectMake(0.0f, 0.0f, arcCenter.x*2, arcCenter.y*2);
  34. _ringAnimatedLayer.fillColor = [UIColor clearColor].CGColor;
  35. _ringAnimatedLayer.strokeColor = self.strokeColor.CGColor;
  36. _ringAnimatedLayer.lineWidth = self.strokeThickness;
  37. _ringAnimatedLayer.lineCap = kCALineCapRound;
  38. _ringAnimatedLayer.lineJoin = kCALineJoinBevel;
  39. _ringAnimatedLayer.path = smoothedPath.CGPath;
  40. }
  41. return _ringAnimatedLayer;
  42. }
  43. - (void)setFrame:(CGRect)frame {
  44. if(!CGRectEqualToRect(frame, super.frame)) {
  45. [super setFrame:frame];
  46. if(self.superview) {
  47. [self layoutAnimatedLayer];
  48. }
  49. }
  50. }
  51. - (void)setRadius:(CGFloat)radius {
  52. if(radius != _radius) {
  53. _radius = radius;
  54. [_ringAnimatedLayer removeFromSuperlayer];
  55. _ringAnimatedLayer = nil;
  56. if(self.superview) {
  57. [self layoutAnimatedLayer];
  58. }
  59. }
  60. }
  61. - (void)setStrokeColor:(UIColor*)strokeColor {
  62. _strokeColor = strokeColor;
  63. _ringAnimatedLayer.strokeColor = strokeColor.CGColor;
  64. }
  65. - (void)setStrokeThickness:(CGFloat)strokeThickness {
  66. _strokeThickness = strokeThickness;
  67. _ringAnimatedLayer.lineWidth = _strokeThickness;
  68. }
  69. - (void)setStrokeEnd:(CGFloat)strokeEnd {
  70. _strokeEnd = strokeEnd;
  71. _ringAnimatedLayer.strokeEnd = _strokeEnd;
  72. }
  73. - (CGSize)sizeThatFits:(CGSize)size {
  74. return CGSizeMake((self.radius+self.strokeThickness/2+5)*2, (self.radius+self.strokeThickness/2+5)*2);
  75. }
  76. @end