IOS 开发自定义条形ProgressView的实例

时间:2022-08-24 10:47:10

IOS 自定义进度条 ProgressView,好的进度条,让人赏心悦目,在等待的时候不是那么烦躁,也算是增加用户体验吧!

进度条在iOS开发中很常见的,我在项目开发中也写过好多进度条,有好多种类的,条形,圆形等,今天给大家总结一种条形的开发进度条。

简单思路:

 1.自定义进度条先继承UIView 建立一个CustomBarProgressView
 2.在.H文件中外漏的方法《开始的方法》《初始化的方法》
 3.在.M文件中 利用定时器改变位置 实现进度条

#效果图

IOS 开发自定义条形ProgressView的实例

#部分代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-(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:可以自己增加 进度条文字等修改大小 样式

别小看任何人,越不起眼的人。往往会做些让人想不到的事。。。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!