欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-18 14:59:28
ios 自定义进度条 progressview,好的进度条,让人赏心悦目,在等待的时候不是那么烦躁,也算是增加用户体验吧! 进度条在ios开发中很常见的,我在项目开发中也...

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

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

简单思路:

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

#效果图

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

#部分代码

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

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

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

上一篇:

下一篇: