PGPickerView.m
20.4 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//
// PGPickerView.m
// PGPickerView
//
// Created by piggybear on 2017/7/26.
// Copyright © 2017年 piggybear. All rights reserved.
//
#import "PGPickerView.h"
#import "PGPickerColumnView.h"
@interface PGPickerView()<PGPickerColumnViewDelegate> {
BOOL _isSubViewLayout;
BOOL _isSelected;
}
@property (nonatomic, strong) NSMutableArray<NSNumber *> *animationOfSelectedRowList;
@property (nonatomic, strong) NSMutableArray<NSNumber *> *numberOfSelectedRowList;
@property (nonatomic, strong) NSMutableArray<NSNumber *> *numberOfSelectedComponentList;
@property (nonatomic, strong) NSArray<UIView *> *upLines;
@property (nonatomic, strong) NSArray<UIView *> *downLines;
@property (nonatomic, assign) NSUInteger numberOfRows;
@property (nonatomic, strong) NSArray<PGPickerColumnView *> *columnViewList;
@end
#define kWidth self.frame.size.width
#define kHeight self.frame.size.height
@implementation PGPickerView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setup];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (_isSubViewLayout) {
return;
}
_isSubViewLayout = true;
[self setupColumnView];
[self setupView];
[self.upLines enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self bringSubviewToFront:obj];
[self bringSubviewToFront:self.downLines[idx]];
}];
if (_isSelected) {
[self.numberOfSelectedRowList enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSInteger component = [self.numberOfSelectedComponentList[idx] integerValue];
BOOL isAnimation = [self.animationOfSelectedRowList[idx] boolValue];
[self selectRow:[obj integerValue] inComponent:component animated:isAnimation];
}];
}
}
- (void)setup {
self.lineBackgroundColor = [UIColor lightGrayColor];
self.textColorOfSelectedRow = [UIColor blackColor];
self.textColorOfOtherRow = [UIColor lightGrayColor];
self.isHiddenMiddleText = true;
self.lineHeight = 0.5;
self.verticalLineWidth = 0.5;
self.verticalLineBackgroundColor = self.lineBackgroundColor;
}
- (PGPickerColumnView *)createColumnViewAtComponent:(NSUInteger)component refresh:(BOOL)refresh {
CGFloat width = kWidth / _numberOfComponents;
NSUInteger row = [self.dataSource pickerView:self numberOfRowsInComponent:component];
NSMutableArray *datas = [NSMutableArray arrayWithCapacity:row];
NSMutableArray *colors = [NSMutableArray arrayWithCapacity:row];
for (int j = 0; j < row; j++) {
BOOL tf = true;
NSAttributedString *attriStr = [[NSAttributedString alloc]initWithString:@"?"];
UIColor *color = [UIColor clearColor];
if (self.delegate) {
if ([self.delegate respondsToSelector:@selector(pickerView:attributedTitleForRow:forComponent:)]) {
attriStr = [self.delegate pickerView:self attributedTitleForRow:j forComponent:component];
if (!attriStr) {
tf = false;
}
}else {
tf = false;
}
if (!tf && [self.delegate respondsToSelector:@selector(pickerView:titleForRow:forComponent:)]) {
NSString *title = [self.delegate pickerView:self titleForRow:j forComponent:component];
title = title ? title : @"?";
attriStr = [[NSAttributedString alloc]initWithString:title];
}
if ([self.delegate respondsToSelector:@selector(pickerView:viewBackgroundColorForRow:forComponent:)]) {
UIColor *temp = [self.delegate pickerView:self viewBackgroundColorForRow:j forComponent:component];
if (temp) {
color = temp;
}
}
}
[colors addObject:color];
[datas addObject:attriStr];
}
// if (component == 1) {
// NSAttributedString *attriStr = [[NSAttributedString alloc]initWithString:@"不限"];
// [datas insertObject:attriStr atIndex:0];
// }
PGPickerColumnView *view = [self columnViewInComponent:component];
if (!view) {
CGFloat rowHeight = [self rowHeightInComponent:component];
CGFloat upLineHeight = [self upLineHeightForComponent:component];
CGFloat downLineHeight = [self downLineHeightForComponent:component];
view = [[PGPickerColumnView alloc]initWithFrame:CGRectMake(component * width, 0, width, kHeight) rowHeight: rowHeight upLineHeight:upLineHeight downLineHeight:downLineHeight];
view.delegate = self;
[self addSubview:view];
}
view.refresh = refresh;
view.viewBackgroundColors = colors;
view.textFontOfSelectedRow = [self textFontOfSelectedRowInComponent:component];
view.textFontOfOtherRow = [self textFontOfOtherRowInComponent:component];
view.component = component;
view.datas = datas;
view.textColorOfSelectedRow = [self textColorOfSelectedRowInComponent:component];
view.textColorOfOtherRow = [self textColorOfOtherRowInComponent:component];
return view;
}
- (void)setupColumnView {
NSMutableArray *columnViewList = [NSMutableArray arrayWithCapacity:_numberOfComponents];
for (int i = 0; i < _numberOfComponents; i++) {
PGPickerColumnView *view = [self createColumnViewAtComponent:i refresh:false];
[columnViewList addObject:view];
}
self.columnViewList = columnViewList;
}
- (void)setupView {
if (!self.isHiddenMiddleText) {
[self setupMiddleTextLogic];
}
switch (self.type) {
case PGPickerViewType1:
{
[self setupLineView1];
return;
}
case PGPickerViewType2:
{
[self setupLineView2];
return;
}
case PGPickerViewType3:
{
[self setupLineView3];
return;
}
}
}
- (void)setupLineView1 {
NSMutableArray *upLines = [NSMutableArray arrayWithCapacity:_numberOfComponents];
NSMutableArray *downLines = [NSMutableArray arrayWithCapacity:_numberOfComponents];
CGFloat lineWidth = (kWidth / _numberOfComponents);
for (int i = 0; i < _numberOfComponents; i++) {
CGFloat rowHeight = [self rowHeightInComponent:i];
CGFloat upLineHeight = [self upLineHeightForComponent:i];
CGFloat upLinePosY = kHeight / 2 - rowHeight / 2 - upLineHeight;
UIView *upLine = [[UIView alloc]initWithFrame:CGRectMake(i * lineWidth, upLinePosY, lineWidth, upLineHeight)];
upLine.backgroundColor = self.lineBackgroundColor;
[self addSubview:upLine];
[upLines addObject:upLine];
CGFloat downLineHeight = [self downLineHeightForComponent:i];
CGFloat downLinePosY = CGRectGetMaxY(upLine.frame) + rowHeight;
UIView *downLine = [[UIView alloc]initWithFrame:CGRectMake(i * lineWidth, downLinePosY, lineWidth, downLineHeight)];
downLine.backgroundColor = self.lineBackgroundColor;
[self addSubview:downLine];
[downLines addObject:downLine];
}
self.upLines = upLines;
self.downLines = downLines;
}
- (void)setupLineView2 {
NSMutableArray *upLines = [NSMutableArray arrayWithCapacity:_numberOfComponents];
NSMutableArray *downLines = [NSMutableArray arrayWithCapacity:_numberOfComponents];
CGFloat lineWidth = (self.frame.size.width / _numberOfComponents) - 20;
CGFloat space = (self.frame.size.width / _numberOfComponents);
if (_numberOfComponents == 1) {
CGFloat rowHeight = [self rowHeightInComponent:0];
CGFloat upLineHeight = [self upLineHeightForComponent:0];
upLineHeight = upLineHeight > 1.5 ? upLineHeight: 1.5;
CGFloat upLinePosY = kHeight / 2 - rowHeight / 2 - upLineHeight;
UIView *upLine = [[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width / 2 - 43, upLinePosY, 90, upLineHeight)];
upLine.backgroundColor = self.lineBackgroundColor;
[self addSubview:upLine];
[upLines addObject:upLine];
CGFloat downLineHeight = [self downLineHeightForComponent:0];
downLineHeight = downLineHeight > 1.5 ? downLineHeight: 1.5;
CGFloat downLinePosY = CGRectGetMaxY(upLine.frame) + rowHeight;
UIView *downLine = [[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width / 2 - 43, downLinePosY, 90, downLineHeight)];
downLine.backgroundColor = self.lineBackgroundColor;
[self addSubview:downLine];
[downLines addObject:downLine];
self.upLines = upLines;
self.downLines = downLines;
return;
}
for (int i = 0; i < _numberOfComponents; i++) {
CGFloat rowHeight = [self rowHeightInComponent:i];
CGFloat upLineHeight = [self upLineHeightForComponent:i];
upLineHeight = upLineHeight > 1.5 ? upLineHeight: 1.5;
CGFloat upLinePosY = kHeight / 2 - rowHeight / 2 - upLineHeight;
UIView *upLine = [[UIView alloc]initWithFrame:CGRectMake(10 + space * i, upLinePosY, lineWidth, upLineHeight)];
upLine.backgroundColor = [self upLineBackgroundColorForComponent:i];
[self addSubview:upLine];
[upLines addObject:upLine];
CGFloat downLineHeight = [self downLineHeightForComponent:i];
downLineHeight = downLineHeight > 1.5 ? downLineHeight: 1.5;
CGFloat downLinePosY = CGRectGetMaxY(upLine.frame) + rowHeight;
UIView *downLine = [[UIView alloc]initWithFrame:CGRectMake(10 + space * i, downLinePosY, lineWidth, downLineHeight)];
downLine.backgroundColor = [self downLineBackgroundColorForComponent:i];
[self addSubview:downLine];
[downLines addObject:downLine];
}
self.upLines = upLines;
self.downLines = downLines;
}
- (void)setupLineView3 {
CGFloat space = (self.frame.size.width / _numberOfComponents);
[self setupLineView2];
for (int i = 0; i < _numberOfComponents; i++) {
if (i != _numberOfComponents - 1) {
CGFloat width = [self verticalLineWidthForComponent:i];
UIView *verticalLine = [[UIView alloc]initWithFrame:CGRectMake(space * (i+1), 0, width, kHeight)];
verticalLine.backgroundColor = [self verticalLineBackgroundColorForComponent:i];
[self addSubview:verticalLine];
}
}
}
- (void)setupMiddleTextLogic {
CGFloat lineWidth = (self.frame.size.width / _numberOfComponents);
CGFloat space = 10;
for (int i = 0; i < _numberOfComponents; i++) {
UILabel *label = [[UILabel alloc]init];
if ([self.delegate respondsToSelector:@selector(pickerView:middleTextSpaceForcomponent:)]) {
space = [self.delegate pickerView:self middleTextSpaceForcomponent:i];
}
label.frame = CGRectMake(lineWidth / 2 + lineWidth * i + space, kHeight / 2 - 15, 30, 30);
NSString *text = @"";
if ([self.delegate respondsToSelector:@selector(pickerView:middleTextForcomponent:)]) {
text = [self.delegate pickerView:self middleTextForcomponent:i];
}
label.text = text;
label.textColor = self.middleTextColor;
label.font = self.middleTextFont;
[self addSubview:label];
}
}
- (UIColor *)verticalLineBackgroundColorForComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:verticalLineBackgroundColorForComponent:)]) {
return [self.delegate pickerView:self verticalLineBackgroundColorForComponent:component];
}
return self.verticalLineBackgroundColor;
}
- (CGFloat)verticalLineWidthForComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:verticalLineWidthForComponent:)]) {
return [self.delegate pickerView:self verticalLineWidthForComponent:component];
}
return self.verticalLineWidth;
}
- (UIColor *)upLineBackgroundColorForComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:upLineBackgroundColorForComponent:)]) {
return [self.delegate pickerView:self upLineBackgroundColorForComponent:component];
}
return self.lineBackgroundColor;
}
- (UIColor *)downLineBackgroundColorForComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:downLineBackgroundColorForComponent:)]) {
return [self.delegate pickerView:self downLineBackgroundColorForComponent:component];
}
return self.lineBackgroundColor;
}
- (CGFloat)upLineHeightForComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:upLineHeightForComponent:)]) {
return [self.delegate pickerView:self upLineHeightForComponent:component];
}
return self.lineHeight;
}
- (CGFloat)downLineHeightForComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:downLineHeightForComponent:)]) {
return [self.delegate pickerView:self downLineHeightForComponent:component];
}
return self.lineHeight;
}
- (UIColor *)textColorOfSelectedRowInComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:textColorOfSelectedRowInComponent:)]) {
return [self.delegate pickerView:self textColorOfSelectedRowInComponent:component];
}
return self.textColorOfSelectedRow;
}
- (UIColor *)textColorOfOtherRowInComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:textColorOfOtherRowInComponent:)]) {
return [self.delegate pickerView:self textColorOfOtherRowInComponent:component];
}
return self.textColorOfOtherRow;
}
- (NSInteger)numberOfRowsInComponent:(NSInteger)component {
if (self.dataSource && [self.dataSource respondsToSelector:@selector(pickerView:numberOfRowsInComponent:)]) {
return [self.dataSource pickerView:self numberOfRowsInComponent:component];
}
return 0;
}
- (NSInteger)selectedRowInComponent:(NSInteger)component {
PGPickerColumnView *view = [self columnViewInComponent:component];
if (view) {
return view.selectedRow;
}
return -1;
}
- (NSString *)textOfSelectedRowInComponent:(NSInteger)component {
PGPickerColumnView *view = [self columnViewInComponent:component];
if (view) {
return view.textOfSelectedRow;
}
return nil;
}
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated {
if (_isSubViewLayout) {
PGPickerColumnView *view = [self columnViewInComponent:component];
[view selectRow:row animated:animated];
return;
}
if (!_isSubViewLayout) {
[self.numberOfSelectedComponentList addObject:@(component)];
[self.numberOfSelectedRowList addObject:@(row)];
[self.animationOfSelectedRowList addObject:@(animated)];
}
_isSelected = true;
}
- (CGFloat)rowHeightInComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(rowHeightInPickerView:forComponent:)]) {
return [self.delegate rowHeightInPickerView:self forComponent: component];
}else if (self.rowHeight != 0) {
return self.rowHeight;
}
self.rowHeight = 44;
return self.rowHeight;
}
- (UIFont *)textFontOfSelectedRowInComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:textFontOfSelectedRowInComponent:)]) {
return [self.delegate pickerView:self textFontOfSelectedRowInComponent:component];
}
return self.textFontOfSelectedRow;
}
- (UIFont *)textFontOfOtherRowInComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:textFontOfOtherRowInComponent:)]) {
return [self.delegate pickerView:self textFontOfOtherRowInComponent:component];
}
return self.textFontOfOtherRow;
}
- (PGPickerColumnView *)columnViewInComponent:(NSUInteger)component {
if (!self.columnViewList || self.columnViewList.count == 0) {
return nil;
}
__block PGPickerColumnView *view = nil;
[self.columnViewList enumerateObjectsUsingBlock:^(PGPickerColumnView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.component == component) {
view = obj;
*stop = true;
}
}];
return view;
}
- (void)reloadComponent:(NSInteger)component {
[self createColumnViewAtComponent:component refresh:false];
}
- (void)reloadComponent:(NSInteger)component refresh:(BOOL)refresh {
[self createColumnViewAtComponent:component refresh:refresh];
}
- (void)reloadAllComponents {
[self setupColumnView];
}
#pragma mark - PGPickerColumnViewDelegate
- (void)pickerColumnView:(PGPickerColumnView *)pickerColumnView didSelectRow:(NSInteger)row {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:didSelectRow:inComponent:)]) {
[self.delegate pickerView:self didSelectRow:row inComponent:pickerColumnView.component];
}
}
- (void)pickerColumnView:(PGPickerColumnView *)pickerColumnView title:(NSString *)title didSelectRow:(NSInteger)row {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:title:didSelectRow:inComponent:)]) {
[self.delegate pickerView:self title:title didSelectRow:row inComponent:pickerColumnView.component];
}
}
- (UIFont *)pickerColumnView:(PGPickerColumnView *)pickerColumnView textFontOfOtherRow:(NSInteger)row InComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:textFontOfOtherRow:InComponent:)]) {
return [self.delegate pickerView:self textFontOfOtherRow:row InComponent:component];
}
return self.textFontOfOtherRow;
}
- (UIColor *)pickerColumnView:(PGPickerColumnView *)pickerColumnView textColorOfOtherRow:(NSInteger)row InComponent:(NSInteger)component {
if (self.delegate && [self.delegate respondsToSelector:@selector(pickerView:textColorOfOtherRow:InComponent:)]) {
return [self.delegate pickerView:self textColorOfOtherRow:row InComponent:component];
}
return self.textColorOfOtherRow;
}
#pragma mark - Setter
- (void)setDataSource:(id<PGPickerViewDataSource>)dataSource {
_dataSource = dataSource;
if (dataSource && [dataSource respondsToSelector:@selector(numberOfComponentsInPickerView:)]) {
_numberOfComponents = [dataSource numberOfComponentsInPickerView:self];
}
}
- (void)setTextColorOfSelectedRow:(UIColor *)textColorOfSelectedRow {
_textColorOfSelectedRow = textColorOfSelectedRow;
[self.columnViewList enumerateObjectsUsingBlock:^(PGPickerColumnView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.textColorOfSelectedRow = textColorOfSelectedRow;
}];
}
- (void)setTextColorOfOtherRow:(UIColor *)textColorOfOtherRow {
_textColorOfOtherRow = textColorOfOtherRow;
[self.columnViewList enumerateObjectsUsingBlock:^(PGPickerColumnView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.textColorOfOtherRow = textColorOfOtherRow;
}];
}
- (void)setLineBackgroundColor:(UIColor *)lineBackgroundColor {
_lineBackgroundColor = lineBackgroundColor;
[self.upLines enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.backgroundColor = lineBackgroundColor;
self.downLines[idx].backgroundColor = lineBackgroundColor;
}];
}
#pragma mark - Getter
- (NSUInteger)numberOfRows {
if (!_numberOfRows) {
if (self.dataSource && [self.dataSource respondsToSelector:@selector(pickerView:numberOfRowsInComponent:)]) {
_numberOfRows = [self.dataSource pickerView:self numberOfRowsInComponent:_numberOfComponents];
}else {
_numberOfRows = 0;
}
}
return _numberOfRows;
}
- (NSMutableArray<NSNumber *> *)animationOfSelectedRowList {
if (!_animationOfSelectedRowList) {
_animationOfSelectedRowList = [NSMutableArray array];
}
return _animationOfSelectedRowList;
}
- (NSMutableArray<NSNumber *> *)numberOfSelectedRowList {
if (!_numberOfSelectedRowList) {
_numberOfSelectedRowList = [NSMutableArray array];
}
return _numberOfSelectedRowList;
}
- (NSMutableArray<NSNumber *> *)numberOfSelectedComponentList {
if (!_numberOfSelectedComponentList) {
_numberOfSelectedComponentList = [NSMutableArray array];
}
return _numberOfSelectedComponentList;
}
@end