当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS如何自定义步骤进度条实例详解

iOS如何自定义步骤进度条实例详解

2019年07月23日  | 移动技术网移动技术  | 我要评论

前言

最近新项目要做入驻功能,其中包括一个入住流程,类似登录或者注册流程如下图。

之前想着用自己绘图来做,可是又懒不想多写代码,所以就想着能不能用进度条来做。

实现方法如下:

1.用进度条做的首先要解决的是进度条的高度问题,可以通过仿射变换来扩大高度。

progressview.transform = cgaffinetransformmakescale(1.0f,2.0f);

2.用进度条要设置进度progress要与按钮对应

通过步骤的索引来改变进度的值和按钮的图片。由于按钮的左右有间隔所以要注意-1、0和最后一个的progress值。

3.扩展

看有一些类似查公交、车站运行的app有的可以点击站点,所以就用按钮来做,这样可以扩展。

 4.代码

//
// stepprogressview.h
// customprogress
//
// created by city--online on 15/12/12.
// copyright © 2015年 city--online. all rights reserved.
//

#import <uikit/uikit.h>

@interface stepprogressview : uiview

@property (nonatomic,assign) nsinteger stepindex;

+(instancetype)progressviewframe:(cgrect)frame withtitlearray:(nsarray *)titlearray;

@end
//
// stepprogressview.m
// customprogress
//
// created by city--online on 15/12/12.
// copyright © 2015年 city--online. all rights reserved.
//

#import "stepprogressview.h"

static const float imgbtnwidth=18;

@interface stepprogressview ()

@property (nonatomic,strong) uiprogressview *progressview;

//用uibutton防止以后有点击事件
@property (nonatomic,strong) nsmutablearray *imgbtnarray;

@end

@implementation stepprogressview

+(instancetype)progressviewframe:(cgrect)frame withtitlearray:(nsarray *)titlearray
{
 stepprogressview *stepprogressview=[[stepprogressview alloc]initwithframe:frame];
 //进度条
 stepprogressview.progressview=[[uiprogressview alloc]initwithframe:cgrectmake(0, 5, frame.size.width, 10)];
 stepprogressview.progressview.progressviewstyle=uiprogressviewstylebar;
 stepprogressview.progressview.transform = cgaffinetransformmakescale(1.0f,2.0f);
 stepprogressview.progressview.progresstintcolor=[uicolor redcolor];
 stepprogressview.progressview.tracktintcolor=[uicolor bluecolor];
 stepprogressview.progressview.progress=0.5;
 [stepprogressview addsubview:stepprogressview.progressview];
 
 
 stepprogressview.imgbtnarray=[[nsmutablearray alloc]init];
 float _btnwidth=frame.size.width/(titlearray.count);
 for (int i=0; i<titlearray.count; i++) {
  //图片按钮
 uibutton *btn=[uibutton buttonwithtype:uibuttontypecustom];
 [btn setimage:[uiimage imagenamed:@"0.png"] forstate:uicontrolstatenormal];
 [btn setimage:[uiimage imagenamed:@"1.png"] forstate:uicontrolstateselected];
 btn.frame=cgrectmake(_btnwidth/2+_btnwidth*i-imgbtnwidth/2, 0, imgbtnwidth, imgbtnwidth);
 btn.selected=yes;
 
 [stepprogressview addsubview:btn];
 [stepprogressview.imgbtnarray addobject:btn];
 
 //文字
 uilabel *titlelabel=[[uilabel alloc]initwithframe:cgrectmake(btn.center.x-_btnwidth/2, frame.size.height-20, _btnwidth, 20)];
 titlelabel.text=[titlearray objectatindex:i];
 [titlelabel settextcolor:[uicolor blackcolor]];
 titlelabel.textalignment=nstextalignmentcenter;
 titlelabel.font=[uifont systemfontofsize:18];
 [stepprogressview addsubview:titlelabel];
 }
 stepprogressview.stepindex=-1;
 return stepprogressview;
 
}
-(void)setstepindex:(nsinteger)stepindex
{
// 默认为-1 小于-1为-1 大于总数为总数
 _stepindex=stepindex<-1?-1:stepindex;
 _stepindex=stepindex >=_imgbtnarray.count-1?_imgbtnarray.count-1:stepindex;
 float _btnwidth=self.bounds.size.width/(_imgbtnarray.count);
 for (int i=0; i<_imgbtnarray.count; i++) {
 uibutton *btn=[_imgbtnarray objectatindex:i];
 if (i<=_stepindex) {
  btn.selected=yes;
 }
 else{
  btn.selected=no;
 }
 }
 if (_stepindex==-1) {
 self.progressview.progress=0.0;
 }
 else if (_stepindex==_imgbtnarray.count-1)
 {
 self.progressview.progress=1.0;
 }
 else
 {
 self.progressview.progress=(0.5+_stepindex)*_btnwidth/self.frame.size.width;
 }
}

@end

5.使用和效果

nsarray *arr=@[@"区宝时尚",@"区宝时尚",@"时尚",@"区宝时尚",@"时尚"];
 stepprogressview *stepview=[stepprogressview progressviewframe:cgrectmake(0, 100, self.view.bounds.size.width, 60) withtitlearray:arr];
 stepview.stepindex=2;
 [self.view addsubview:stepview];

 补充:上面的代码有一个bug,例如stepindex=-1时,_stepindex=并不等-1,原来数组的count返回的是nsuinteger而stepindex是nsinteger类型,所以需要强制转换一下

stepindex=stepindex<-1?-1:stepindex;
 _stepindex = stepindex >= (nsinteger)(_imgbtnarray.count-1) ? _imgbtnarray.count-1:stepindex;

总结:

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网