当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS 开发自定义条形ProgressView的实例

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

黄望晴,再一次心动,乌鲁木齐之夜3

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

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

简单思路:

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

#效果图

#部分代码

-(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:可以自己增加 进度条文字等修改大小 样式

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网