-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZXYSegmentedControl.m
227 lines (102 loc) · 3.84 KB
/
ZXYSegmentedControl.m
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// ZXYSegmentedControl.m
//
// Created by Rockeen on 15/11/28.
// Copyright (c) 2015年 Rockeen https://github.com/rockeen. All rights reserved.
//
#import "ZXYSegmentedControl.h"
@implementation ZXYSegmentedControl{
UIButton *_lastBtn;//上一个btn
CGFloat with;//btn宽度
UIImageView *_selectImageV;//选中图片的imagev
PageNum blockTwo;//负责接收值的block
//正常状态下
UIColor *normalColor;
CGFloat normalTitleSize;
//选中状态下
UIColor *selectColor;
CGFloat selectTitleSize;
}
//初始化方法
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles{
if (self=[super initWithFrame:frame]) {
self.titles=titles;
with=self.frame.size.width/titles.count;
[self variable];
[self creatImageV];
[self creatSegmentControl:titles];
}
return self;
}
//初始化变量
- (void)variable{
//正常状态下
normalColor=[UIColor whiteColor];
normalTitleSize=20;
//选中状态下
selectColor=[UIColor orangeColor];
selectTitleSize=25;
}
//创建SegmentControl给view上加上button
- (void)creatSegmentControl:(NSArray *)title{
for (int i=0; i<title.count; i++) {
CGFloat x=i*with;
CGFloat y=0;
CGFloat width=with;
CGFloat height=self.frame.size.height;
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
[btn setTitle:title[i] forState:UIControlStateNormal];
[btn setTitleColor:normalColor forState:UIControlStateNormal];
[btn setTitleColor:selectColor forState:UIControlStateSelected];
btn.titleLabel.font=[UIFont systemFontOfSize:normalTitleSize];
[btn addTarget:self action:@selector(btnAct:) forControlEvents:UIControlEventTouchUpInside];
btn.tag=100+i;
[self addSubview:btn];
if (i==0) {
btn.selected=YES;
btn.titleLabel.font=[UIFont systemFontOfSize:selectTitleSize];
_lastBtn=btn;
//选中图片的位置
_selectImageV.frame=CGRectMake(btn.bounds.origin.x, self.frame.size.height-3, with, 3);
}
}
}
//创建imageV
- (void)creatImageV{
_selectImageV=[[UIImageView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-3, with, 3)];
[self addSubview:_selectImageV];
}
//btn点击事件
- (void)btnAct:(UIButton *)sender{
if (_lastBtn!=sender) {
_lastBtn.selected=NO;
_lastBtn.titleLabel.font=[UIFont systemFontOfSize:normalTitleSize];
sender.selected=YES;
sender.titleLabel.font=[UIFont systemFontOfSize:selectTitleSize];
//设置选中图片的位置
[UIView animateWithDuration:.3 animations:^{
_selectImageV.transform=CGAffineTransformMakeTranslation(with*(sender.tag-100), 0);
}];
//回调
blockTwo(sender.tag-100);
}
_lastBtn=sender;
}
//block回调
- (void)returnPageNum:(PageNum)block{
blockTwo=block;
}
//选中图片的setter方法
- (void)setSelectImage:(UIImage *)selectImage{
_selectImage=selectImage;
_selectImageV.image=selectImage;
}
//默认选中btn的setter方法
- (void)setFirstIndix:(NSInteger)firstIndix{
_firstIndix=firstIndix;
if (_firstIndix>=0&&_firstIndix<_titles.count-1) {
UIButton *btn=(UIButton *)[self viewWithTag:100+_firstIndix];
[self btnAct:btn];
}
}
@end