自定義條形ProgressView

2013java 8年前發布 | 8K 次閱讀 iOS開發 移動開發

進度條在iOS開發中很常見的,我在項目開發中也寫過好多進度條,有好多種類的,條形,圓形等,今天給大家總結一種條形的開發進度條。

簡單思路:

1.自定義進度條先繼承UIView 建立一個CustomBarProgressView
2.在.H文件中外漏的方法《開始的方法》《初始化的方法》
3.在.M文件中 利用定時器改變位置 實現進度條

效果圖

1.gif

部分代碼

-(instancetype)initWithFrame:(CGRect)frame withStartNum:(CGFloat)startNum withEndNum:(CGFloat)endNum withSignNum:(CGFloat)signNum withTime:(CGFloat)time{
    if (self = [super initWithFrame:frame]) {

        self.startNum =  startNum;
        self.endNum = endNum;
        self.signNum = signNum;

        if(time == 0){
            self.time  = 0.1;
        }else{
            self.time = time;
        }

        [self setUpSubViews];
    }
    return self;
}

- (void)setUpSubViews
{
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor =BoomViewColor;
    backView.layer.cornerRadius = CornerRadius;
    backView.layer.masksToBounds = YES;
    [self addSubview:backView];
    self.backView = backView;

    UIView *fontView = [[UIView alloc] init];
    fontView.backgroundColor = UpViewColor;
    fontView.layer.cornerRadius = CornerRadius;
    fontView.layer.masksToBounds = YES;
    [self addSubview:fontView];
    self.fontView = fontView;

}

-(void)progressViewStart{
    if (self.timer == nil) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.timer = [NSTimer scheduledTimerWithTimeInterval:self.time target:self selector:@selector(changeProgressViewFrame:) userInfo:nil repeats:YES];
            [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
        });

    }
}

-(void)changeProgressViewFrame:(NSTimer *)timer{

    //位置計算
    CGFloat signProgress = (self.signNum - self.startNum) / (self.endNum - self.startNum);
   NSLog(@"==>>>%f",self.progress);
    if (self.progress >= signProgress){
        [self.timer invalidate];
        self.timer = nil;
        return;
    }

    self.progress += 0.01;
    [self setNeedsLayout];

}

-(void)layoutSubviews{
    [super layoutSubviews];
    NSLog(@"==>>>%f",self.progress);
    self.backView.frame = self.bounds;
    self.fontView.frame = self.bounds;
    self.fontView.width =  self.width * self.progress;

}

PS:可以自己增加 進度條文字等修改大小 樣式

 

 

來自:http://www.jianshu.com/p/f35c5809c41e

 

 本文由用戶 2013java 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!