123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- //
- // KSProgressLoadingView.m
- // KulexiuSchoolStudent
- //
- // Created by 王智 on 2023/11/7.
- //
- #import "KSProgressLoadingView.h"
- @interface KSProgressLoadingView ()
- @property (weak, nonatomic) IBOutlet UIView *progressView;
- @property (weak, nonatomic) IBOutlet UIImageView *progressFillImage;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageWidth;
- @property (weak, nonatomic) IBOutlet UILabel *progressTipsLabel;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *leadingSpace;
- @property (nonatomic, assign) NSInteger progress;
- @end
- @implementation KSProgressLoadingView
- - (void)awakeFromNib {
- [super awakeFromNib];
- self.progress = 0;
- self.leadingSpace.constant = 0.0f;
- }
- + (instancetype)shareInstance {
- KSProgressLoadingView *view = [[[NSBundle mainBundle] loadNibNamed:@"KSProgressLoadingView" owner:nil options:nil] firstObject];
- return view;
- }
- - (void)configProgressWithText:(NSString *)tipsMessage progress:(CGFloat)progress {
- self.progressTipsLabel.text = tipsMessage;
- NSInteger space = progress * 280;
- [UIView animateWithDuration:0.5f animations:^{
- self.progress = space;
- self.leadingSpace.constant = space;
- }];
- }
- - (void)configProgressNoAnimationWithText:(NSString *)tipsMessage progress:(CGFloat)progress {
- self.progressTipsLabel.text = tipsMessage;
- NSInteger space = progress * 280;
- self.progress = space;
- self.leadingSpace.constant = space;
- }
- - (void)configProgressWithText:(NSString *)tipsMessage progress:(CGFloat)progress promptCompletion:(void(^)(void))promptCompletion {
- self.progressTipsLabel.text = tipsMessage;
- NSInteger space = progress * 280;
- self.progress = space;
- self.leadingSpace.constant = space;
- [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
-
- } completion:^(BOOL finished) {
- promptCompletion();
- }];
- }
- - (void)showLoadingView {
- UIWindow *windows = [NSObject getKeyWindow];
- [windows addSubview:self];
- [self mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.top.bottom.mas_equalTo(windows);
- }];
- }
- - (void)hideLoadingView {
- [self removeFromSuperview];
- }
- - (void)setProgress:(NSInteger)progress {
- _progress = progress;
- CGFloat width = progress;
- [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)]];
- self.imageWidth.constant = width;
- }
- - (UIImage *)createGradientImageWithColor:(NSArray *)colors rect:(CGRect)rect start:(CGPoint)start end:(CGPoint)end {
- if (!colors.count || rect.size.width <= 0 || rect.size.height <= 0) {
- return nil;
- }
- CAGradientLayer *gradientLayer = [CAGradientLayer layer];
-
- gradientLayer.frame = rect;
- gradientLayer.startPoint = start;
- gradientLayer.endPoint = end;
- NSMutableArray *mutColors = [NSMutableArray arrayWithCapacity:colors.count];
- for (UIColor *color in colors) {
- [mutColors addObject:(__bridge id)color.CGColor];
- }
- gradientLayer.colors = [NSArray arrayWithArray:mutColors];
-
- UIGraphicsBeginImageContextWithOptions(gradientLayer.frame.size, gradientLayer.opaque, 0);
- [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return outputImage;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|