美文网首页
iOS 封装的一个简单的进度条

iOS 封装的一个简单的进度条

作者: CMD独白 | 来源:发表于2017-06-08 14:22 被阅读155次

    自己封装的一个简单的进度条,背景是图片,代码如下:

    .h里边的代码

    #import <UIKit/UIKit.h>
    
    @interface ProgressImageView : UIView
    /**
     进度值
     */
    @property (nonatomic,assign)NSInteger progressValue;
    
    /**
     进度条的颜色
     */
    @property (nonatomic,strong) UIColor *progressColor;
    
    /**
     进度条的背景图片
     */
    @property (nonatomic,strong)UIImageView *bottomImageView;
    
    //显示进度的视图(改变x的值)
    @property (nonatomic,strong)UIView *fillView;
    
    -(void)setProgressValue:(NSInteger)progressValue AndIndex:(NSInteger)progress;
    
    

    .m里边的代码

    #import "ProgressImageView.h"
    
    @interface ProgressImageView()
    {
        CGFloat fillViewWidth;
        CGFloat lineViewLeft;
    }
    @end
    @implementation ProgressImageView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
            
            [self buildUI];
            
        }
        return self;
    }
    
    - (void)buildUI{
    
        _bottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, self.width, self.height-4)];
        _bottomImageView.layer.cornerRadius = 3;
        _bottomImageView.layer.masksToBounds = YES;
        _bottomImageView.clipsToBounds = YES;
        [self addSubview:_bottomImageView];
        
        _fillView = [[UIView alloc]initWithFrame:CGRectMake(0-_bottomImageView.width, 0, _bottomImageView.width, _bottomImageView.height)];
        _fillView.layer.cornerRadius = 3;
        [_bottomImageView addSubview:_fillView];
        
    }
    
    -(void)setProgressValue:(NSInteger)progressValue AndIndex:(NSInteger)progress{
        _progressValue = progressValue;
        CGFloat X = [self getProgress:progress AndValue:progressValue];
    //    fillViewWidth = [[[NSUserDefaults standardUserDefaults] objectForKey:@"fillViewWidth"] floatValue];
    //        NSLog(@"width:%f",fillViewWidth);
    //    if (fillViewWidth > 0) {
    //        _fillView.left = X-_bottomImageView.width;
    //        
    //    }else{
    //    
    //    [UIView animateWithDuration:1 animations:^{
            if (progressValue == 0) {
                _fillView.hidden = YES;
            }else{
         _fillView.hidden = NO;
         _fillView.left = X-_bottomImageView.width;
         fillViewWidth = X;
            
          }
    //    }];
    //        [[NSUserDefaults standardUserDefaults] setObject:@(fillViewWidth) forKey:@"fillViewWidth"];
    //    }
    }
    //进度条的颜色
    - (void)setProgressColor:(UIColor *)progressColor{
    
        _progressColor = progressColor;
        _fillView.backgroundColor = progressColor;
    }
    //得到视图距离左边的位置
    - (CGFloat)getProgress:(NSInteger)progress AndValue:(NSInteger)value{
        
       CGFloat X = (_bottomImageView.width/progress)*value;
        return X;
    }
    

    调用的时候:

     _progressView = [[ProgressImageView alloc]initWithFrame:CGRectMake(_dateLabel.left, _dateLabel.bottom+5, self.moreData.left-_leftImageView.right-10, _dateLabel.height+4)];
        [bgView addSubview:_progressView];
    [ _progressView setProgressValue:当前进度值 AndIndex:进度条的最大进度值];
    
    

    相关文章

      网友评论

          本文标题:iOS 封装的一个简单的进度条

          本文链接:https://www.haomeiwen.com/subject/jbttqxtx.html