在按钮上添加进度条

首先创建一个继承自UIVIew的控件
.h

//
//  FYProgressBar.h
//  2019TestProduct
//
//  Copyright © 2019 ChenFuYou. All rights reserved.
//

#import 

NS_ASSUME_NONNULL_BEGIN

@interface FYProgressBar : UIView

@property(nonatomic,strong)UIView *topView;
@property(nonatomic,strong)UILabel *showStep;

-(void)setProgressFromPercent:(CGFloat)percent1 toPercent:(CGFloat)percent2;
-(void)removeProgress;
@end

NS_ASSUME_NONNULL_END

.m

//
//  FYProgressBar.m
//  2019TestProduct
//
//  Copyright © 2019 ChenFuYou. All rights reserved.
//

#import "FYProgressBar.h"

@implementation FYProgressBar

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self createUI];
        
    }
    return self;
}
-(void)createUI{
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    backView.backgroundColor = [UIColor grayColor];
    backView.layer.cornerRadius = CGRectGetHeight(self.frame)/2;
    [self addSubview:backView];
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, CGRectGetHeight(self.frame))];
    self.topView.backgroundColor = [UIColor colorWithRed:79/255.0 green:138/255.0 blue:255/255.0 alpha:1];
    self.topView.layer.cornerRadius = CGRectGetHeight(self.frame)/2;
    [self addSubview:self.topView];
    
    self.showStep = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    self.showStep.textColor = [UIColor whiteColor];
    self.showStep.layer.masksToBounds = YES;
    self.showStep.text = @"0";
    self.showStep.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.showStep];

}
-(void)setProgressFromPercent:(CGFloat)percent1 toPercent:(CGFloat)percent2{

    [UIView animateWithDuration:0.8 animations:^{
        
        self.showStep.text = [NSString stringWithFormat:@"%.f%%",percent1 * 100];
        self.topView.frame = CGRectMake(self.topView.bounds.origin.x ,
                                self.topView.bounds.origin.y ,
                                percent2 * CGRectGetWidth(self.frame) ,
                                self.topView.bounds.size.height);
    } completion:^(BOOL finished) {
        
        self.showStep.text = [NSString stringWithFormat:@"%.f%%",percent2 * 100];
        
    }];
    

}
-(void)removeProgress{
    for (UIView *subView in self.subviews) {
        [subView removeFromSuperview];
    }
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

使用:
设为属性:

@property (nonatomic, strong) FYProgressBar *progressBar;

懒加载:

-(FYProgressBar *)progressBar{
    if (_progressBar == nil) {
        self.progressBar = [[FYProgressBar alloc] initWithFrame:self.applyBtn.frame];
        [self.view addSubview:self.progressBar];

        [self.progressBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.left.equalTo(self.view).offset(35);
            make.right.equalTo(self.view).offset(-35);
            make.height.offset(40);
            make.bottom.equalTo(self.view).offset(-20);
        }];
    }
    return _progressBar;
}

在需要的地方

    [self.progressBar setProgressFromPercent:0.2 toPercent:0.5];

移除进度条

-(void)progressHide{
    [self.progressBar removeFromSuperview];
}

你可能感兴趣的:(在按钮上添加进度条)