KSProgressLoadingView.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. + (instancetype)shareInstance {
  18. KSProgressLoadingView *view = [[[NSBundle mainBundle] loadNibNamed:@"KSProgressLoadingView" owner:nil options:nil] firstObject];
  19. return view;
  20. }
  21. - (void)configProgressWithText:(NSString *)tipsMessage progress:(NSInteger)progress {
  22. self.progressTipsLabel.text = tipsMessage;
  23. NSInteger space = progress * 1.0 / 100 * 280;
  24. self.progress = space;
  25. [UIView animateWithDuration:0.3f animations:^{
  26. self.leadingSpace.constant = space;
  27. }];
  28. }
  29. - (void)showLoadingView {
  30. UIWindow *windows = [NSObject getKeyWindow];
  31. [windows addSubview:self];
  32. [self mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.left.right.top.bottom.mas_equalTo(windows);
  34. }];
  35. }
  36. - (void)hideLoadingView {
  37. [self removeFromSuperview];
  38. }
  39. - (void)setProgress:(NSInteger)progress {
  40. _progress = progress;
  41. CGFloat width = progress;
  42. [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)]];
  43. self.imageWidth.constant = width;
  44. }
  45. - (UIImage *)createGradientImageWithColor:(NSArray *)colors rect:(CGRect)rect start:(CGPoint)start end:(CGPoint)end {
  46. if (!colors.count || CGRectEqualToRect(rect, CGRectZero)) {
  47. return nil;
  48. }
  49. CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  50. gradientLayer.frame = rect;
  51. gradientLayer.startPoint = start;
  52. gradientLayer.endPoint = end;
  53. NSMutableArray *mutColors = [NSMutableArray arrayWithCapacity:colors.count];
  54. for (UIColor *color in colors) {
  55. [mutColors addObject:(__bridge id)color.CGColor];
  56. }
  57. gradientLayer.colors = [NSArray arrayWithArray:mutColors];
  58. UIGraphicsBeginImageContextWithOptions(gradientLayer.frame.size, gradientLayer.opaque, 0);
  59. [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
  60. UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
  61. UIGraphicsEndImageContext();
  62. return outputImage;
  63. }
  64. /*
  65. // Only override drawRect: if you perform custom drawing.
  66. // An empty implementation adversely affects performance during animation.
  67. - (void)drawRect:(CGRect)rect {
  68. // Drawing code
  69. }
  70. */
  71. @end