FUProgressHUD.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // FUProgressHUD.m
  3. // FULiveDemo
  4. //
  5. // Created by 项林平 on 2022/10/21.
  6. //
  7. #import "FUProgressHUD.h"
  8. #import "FULiveDefine.h"
  9. @interface FUProgressHUD ()
  10. @property (nonatomic, strong) CAShapeLayer *backgroundLayer;
  11. @property (nonatomic, strong) CAShapeLayer *progressLayer;
  12. @property (nonatomic, strong) UILabel *progressLabel;
  13. @end
  14. @implementation FUProgressHUD
  15. - (instancetype)initWithFrame:(CGRect)frame {
  16. self = [super initWithFrame:frame];
  17. if (self) {
  18. [self.layer addSublayer:self.backgroundLayer];
  19. [self.layer addSublayer:self.progressLayer];
  20. [self addSubview:self.progressLabel];
  21. self.progressLabel.center = self.center;
  22. }
  23. return self;
  24. }
  25. - (void)setProgress:(CGFloat)progress {
  26. self.progressLayer.strokeEnd = progress;
  27. NSInteger temp = (NSInteger)(progress * 100);
  28. self.progressLabel.text = [NSString stringWithFormat:@"%ld%%", (long)temp];
  29. }
  30. - (CAShapeLayer *)backgroundLayer {
  31. if (!_backgroundLayer) {
  32. UIBezierPath *smoothedPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:CGRectGetWidth(self.frame) / 2.f startAngle:-M_PI_2 endAngle:(M_PI + M_PI_2) clockwise:YES];
  33. _backgroundLayer = [CAShapeLayer layer];
  34. _backgroundLayer.frame = self.bounds;
  35. _backgroundLayer.fillColor = [UIColor clearColor].CGColor;
  36. _backgroundLayer.strokeColor = FUColorFromHex(0x111226).CGColor;
  37. _backgroundLayer.lineWidth = 4.f;
  38. _backgroundLayer.strokeEnd = 1.f;
  39. _backgroundLayer.path = smoothedPath.CGPath;
  40. }
  41. return _backgroundLayer;
  42. }
  43. - (CAShapeLayer *)progressLayer {
  44. if (!_progressLayer) {
  45. UIBezierPath *smoothedPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:CGRectGetWidth(self.frame) / 2.f startAngle:-M_PI_2 endAngle:(M_PI + M_PI_2) clockwise:YES];
  46. _progressLayer = [CAShapeLayer layer];
  47. _progressLayer.frame = self.bounds;
  48. _progressLayer.fillColor = [UIColor clearColor].CGColor;
  49. _progressLayer.strokeColor = FUColorFromHex(0x5EC7FE).CGColor;
  50. _progressLayer.lineWidth = 4.f;
  51. _progressLayer.lineCap = kCALineCapRound;
  52. _progressLayer.lineJoin = kCALineJoinBevel;
  53. _progressLayer.strokeEnd = 0.f;
  54. _progressLayer.path = smoothedPath.CGPath;
  55. }
  56. return _progressLayer;
  57. }
  58. - (UILabel *)progressLabel {
  59. if (!_progressLabel) {
  60. _progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
  61. _progressLabel.textColor = [UIColor whiteColor];
  62. _progressLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  63. _progressLabel.textAlignment = NSTextAlignmentCenter;
  64. _progressLabel.text = [NSString stringWithFormat:@"0%%"];
  65. }
  66. return _progressLabel;
  67. }
  68. @end