KSProgressLoadingView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // KSProgressLoadingView.m
  3. // KulexiuSchoolStudent
  4. //
  5. // Created by 王智 on 2023/11/7.
  6. //
  7. #import "KSProgressLoadingView.h"
  8. @interface KSProgressLoadingView ()
  9. @property (weak, nonatomic) IBOutlet UIView *progressView;
  10. @property (weak, nonatomic) IBOutlet UIImageView *progressFillImage;
  11. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageWidth;
  12. @property (weak, nonatomic) IBOutlet UILabel *progressTipsLabel;
  13. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *leadingSpace;
  14. @property (nonatomic, assign) NSInteger progress;
  15. @end
  16. @implementation KSProgressLoadingView
  17. - (void)awakeFromNib {
  18. [super awakeFromNib];
  19. self.progress = 0;
  20. self.leadingSpace.constant = 0.0f;
  21. }
  22. + (instancetype)shareInstance {
  23. KSProgressLoadingView *view = [[[NSBundle mainBundle] loadNibNamed:@"KSProgressLoadingView" owner:nil options:nil] firstObject];
  24. return view;
  25. }
  26. - (void)configProgressWithText:(NSString *)tipsMessage progress:(CGFloat)progress {
  27. self.progressTipsLabel.text = tipsMessage;
  28. NSInteger space = progress * 280;
  29. [UIView animateWithDuration:0.5f animations:^{
  30. self.progress = space;
  31. self.leadingSpace.constant = space;
  32. }];
  33. }
  34. - (void)configProgressNoAnimationWithText:(NSString *)tipsMessage progress:(CGFloat)progress {
  35. self.progressTipsLabel.text = tipsMessage;
  36. NSInteger space = progress * 280;
  37. self.progress = space;
  38. self.leadingSpace.constant = space;
  39. }
  40. - (void)configProgressWithText:(NSString *)tipsMessage progress:(CGFloat)progress promptCompletion:(void(^)(void))promptCompletion {
  41. self.progressTipsLabel.text = tipsMessage;
  42. NSInteger space = progress * 280;
  43. self.progress = space;
  44. self.leadingSpace.constant = space;
  45. [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
  46. } completion:^(BOOL finished) {
  47. promptCompletion();
  48. }];
  49. }
  50. - (void)showLoadingView {
  51. UIWindow *windows = [NSObject getKeyWindow];
  52. [windows addSubview:self];
  53. [self mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.left.right.top.bottom.mas_equalTo(windows);
  55. }];
  56. }
  57. - (void)hideLoadingView {
  58. [self removeFromSuperview];
  59. }
  60. - (void)setProgress:(NSInteger)progress {
  61. _progress = progress;
  62. CGFloat width = progress;
  63. [self.progressFillImage setImage:[self createGradientImageWithColor:@[HexRGB(0x44F1D0),HexRGB(0x2DC7AA)] rect:CGRectMake(0, 0, width, 8) start:CGPointMake(0.41, 0) end:CGPointMake(0.41, 1)]];
  64. self.imageWidth.constant = width;
  65. }
  66. - (UIImage *)createGradientImageWithColor:(NSArray *)colors rect:(CGRect)rect start:(CGPoint)start end:(CGPoint)end {
  67. if (!colors.count || rect.size.width <= 0 || rect.size.height <= 0) {
  68. return nil;
  69. }
  70. CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  71. gradientLayer.frame = rect;
  72. gradientLayer.startPoint = start;
  73. gradientLayer.endPoint = end;
  74. NSMutableArray *mutColors = [NSMutableArray arrayWithCapacity:colors.count];
  75. for (UIColor *color in colors) {
  76. [mutColors addObject:(__bridge id)color.CGColor];
  77. }
  78. gradientLayer.colors = [NSArray arrayWithArray:mutColors];
  79. UIGraphicsBeginImageContextWithOptions(gradientLayer.frame.size, gradientLayer.opaque, 0);
  80. [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
  81. UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
  82. UIGraphicsEndImageContext();
  83. return outputImage;
  84. }
  85. /*
  86. // Only override drawRect: if you perform custom drawing.
  87. // An empty implementation adversely affects performance during animation.
  88. - (void)drawRect:(CGRect)rect {
  89. // Drawing code
  90. }
  91. */
  92. @end