苹果原生的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{
//调整进度View的frame
self.progressView.frame =CGRectMake(_progressView.x,_progressView.y,_wholeWidth * _progress,_progressView.height);
//调整右边光view的frame
self.progressRightView.frame =CGRectMake(CGRectGetMaxX(self.progressView.frame) - 50, _progressRightView.y,_progressRightView.width,_progressRightView.height);
//调整上部显示数据view的frame
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.