前言
最近新項目要做入駐功能,其中包括一個入住流程,類似登錄或者注冊流程如下圖。
之前想著用自己繪圖來做,可是又懶不想多寫代碼,所以就想著能不能用進度條來做。
實現方法如下:
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