前言
最近新项目要做入驻功能,其中包括一个入住流程,类似登录或者注册流程如下图。
之前想着用自己绘图来做,可是又懒不想多写代码,所以就想着能不能用进度条来做。
实现方法如下:
1.用进度条做的首先要解决的是进度条的高度问题,可以通过仿射变换来扩大高度。
1
|
progressview.transform = cgaffinetransformmakescale(1.0f,2.0f); |
2.用进度条要设置进度progress要与按钮对应
通过步骤的索引来改变进度的值和按钮的图片。由于按钮的左右有间隔所以要注意-1、0和最后一个的progress值。
3.扩展
看有一些类似查公交、车站运行的app有的可以点击站点,所以就用按钮来做,这样可以扩展。
4.代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// // 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 |
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
// // 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.使用和效果
1
2
3
4
|
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类型,所以需要强制转换一下
1
2
|
stepindex=stepindex<-1?-1:stepindex; _stepindex = stepindex >= (nsinteger)(_imgbtnarray.count-1) ? _imgbtnarray.count-1:stepindex; |
总结:
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:http://www.cnblogs.com/5ishare/p/5044447.html