自定制ProgressView

苹果原生的progressView高度不可变,用起来很是不方便,说不定以后用的到,别人说不定也用的到,还是自己写一个。

下边是主要的代码,详细的代码可以参考https://github.com/gofey/LittleDemos自定义ProgressView这一项

闲话不说,都是比较基础的代码,都能看得懂

我是自定制的一个View

重新initWithFrame方法

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [superinitWithFrame:frame]) {

        

        //初始化


        UIImageView *trackView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];

        [selfaddSubview:trackView];

        self.trackView = trackView;

        trackView.clipsToBounds =YES;

        self.layer.cornerRadius = frame.size.height / 2;

        UIImageView *progressView = [[UIImageViewalloc]initWithFrame:CGRectMake(1,3, frame.size.width -2, frame.size.height -6)];

        [selfaddSubview:progressView];

       //记录原始总宽度,计算进度时候使用

        _wholeWidth = progressView.width;

        self.progressView = progressView;

        progressView.clipsToBounds =YES;

        progressView.layer.cornerRadius = progressView.height /2;

        

        UIImageView *progressRightView = [[UIImageViewalloc]initWithFrame:CGRectMake(progressView.x -33, (frame.size.height -32) /2,33,32)];

        [selfaddSubview:progressRightView];

        self.progressRightView = progressRightView;

        progressRightView.hidden =YES;

        

        UIImageView *progressDataImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,45,30)];

        [selfaddSubview:progressDataImageView];

        self.progressDataImageView = progressDataImageView;

        progressDataImageView.hidden =YES;

        

        UILabel *dataLabel = [[UILabelalloc]initWithFrame:progressDataImageView.bounds];

        [progressDataImageViewaddSubview:dataLabel];

        self.dataLabel = dataLabel;

        dataLabel.textColor = [UIColorwhiteColor];

        dataLabel.font = [UIFontsystemFontOfSize:17];

        dataLabel.textAlignment =NSTextAlignmentCenter;

    }

    returnself;

}



修改进度方法

- (void)setProgress:(CGFloat)progress animated:(BOOL)animated{

    _progress = progress;

    self.dataLabel.text = [NSStringstringWithFormat:@"%.0f",_dataTatal * progress];

    

    if (animated) {

        [UIViewanimateWithDuration:0.3animations:^{

            [selfchageFrame];

        } completion:^(BOOL finished) {

            

        }];

        

    }else{

        [selfchageFrame];

        

    }

}

//改变Frame

- (void)chageFrame{

    //调整进度Viewframe

    self.progressView.frame =CGRectMake(_progressView.x,_progressView.y,_wholeWidth * _progress,_progressView.height);

    //调整右边光viewframe

    self.progressRightView.frame =CGRectMake(CGRectGetMaxX(self.progressView.frame) - 50, _progressRightView.y,_progressRightView.width,_progressRightView.height);

    //调整上部显示数据viewframe

    self.progressDataImageView.frame =CGRectMake(CGRectGetMaxX(self.progressView.frame), - _progressDataImageView.height -5,_progressDataImageView.width,_progressDataImageView.height);

    self.dataLabel.text = [NSStringstringWithFormat:@"%.0f",self.dataTatal *_progress];

}


改变progress的时候,不需要动画就调用以上方法,animated值为NO,需要动画为Yes.


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值