forked from kreeger/BDKCollectionIndexView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBDKCollectionIndexView.m
229 lines (179 loc) · 7.37 KB
/
BDKCollectionIndexView.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
228
229
#import "BDKCollectionIndexView.h"
#import <QuartzCore/QuartzCore.h>
@interface BDKCollectionIndexView ()
/**
A component that shows up under the letters to indicate the view is handling a touch or a pan.
*/
@property (strong, nonatomic) UIView *touchStatusView;
/**
The collection of label subviews that are displayed (one for each index title).
*/
@property (strong, nonatomic) NSArray *indexLabels;
/**
A gesture recognizer that handles panning.
*/
@property (strong, nonatomic) UIPanGestureRecognizer *panner;
/**
A gesture recognizer that handles tapping.
*/
@property (strong, nonatomic) UITapGestureRecognizer *tapper;
@property (readonly) CGFloat theDimension;
/**
Handles events sent by the tap gesture recognizer.
@param recognizer the sender of the event; usually a UIPanGestureRecognizer.
*/
- (void)handleTap:(UITapGestureRecognizer *)recognizer;
/**
Handles events sent by the pan gesture recognizer.
@param recognizer the sender of the event; usually a UIPanGestureRecognizer.
*/
- (void)handlePan:(UIPanGestureRecognizer *)recognizer;
/**
Handles logic for determining which label is under a given touch point, and sets `currentIndex` accordingly.
@param point the touch point.
*/
- (void)setNewIndexForPoint:(CGPoint)point;
/**
Handles setting the alpha component level for the background color on the `touchStatusView`.
@param flag if `YES`, the `touchStatusView` is set to be visible and dark-ish.
*/
- (void)setBackgroundVisibility:(BOOL)flag;
@end
@implementation BDKCollectionIndexView
@synthesize currentIndex = _currentIndex, direction = _direction, theDimension = _theDimension, labelColor = _labelColor, backgroundColor = _backgroundColor;
+ (instancetype)indexViewWithFrame:(CGRect)frame indexTitles:(NSArray *)indexTitles {
return [[self alloc] initWithFrame:frame indexTitles:indexTitles];
}
- (instancetype)initWithFrame:(CGRect)frame indexTitles:(NSArray *)indexTitles {
self = [super initWithFrame:frame];
if (!self) return nil;
if (CGRectGetWidth(frame) > CGRectGetHeight(frame))
_direction = BDKCollectionIndexViewDirectionHorizontal;
else _direction = BDKCollectionIndexViewDirectionVertical;
_currentIndex = 0;
_endPadding = 2;
_labelColor = [UIColor blackColor];
_backgroundColor = [UIColor clearColor];
_panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self addGestureRecognizer:_panner];
_tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:_tapper];
[self addSubview:self.touchStatusView];
self.indexTitles = indexTitles;
return self;
}
- (void)layoutSubviews {
CGFloat maxLength = 0.0;
switch (_direction) {
case BDKCollectionIndexViewDirectionHorizontal:
_theDimension = CGRectGetHeight(self.frame);
maxLength = CGRectGetWidth(self.frame) - (self.endPadding * 2);
break;
case BDKCollectionIndexViewDirectionVertical:
_theDimension = CGRectGetWidth(self.frame);
maxLength = CGRectGetHeight(self.frame) - (self.endPadding * 2);
break;
}
self.touchStatusView.frame = CGRectInset(self.bounds, 2, 2);
self.touchStatusView.layer.cornerRadius = floorf(self.theDimension / 2.75);
CGFloat cumulativeLength = self.endPadding;
CGSize labelSize = CGSizeMake(self.theDimension, self.theDimension);
CGFloat otherDimension = floorf(maxLength / self.indexLabels.count);
for (UILabel *label in self.indexLabels) {
switch (self.direction) {
case BDKCollectionIndexViewDirectionHorizontal:
labelSize.width = otherDimension;
label.frame = (CGRect){ { cumulativeLength, 0 }, labelSize };
cumulativeLength += CGRectGetWidth(label.frame);
break;
case BDKCollectionIndexViewDirectionVertical:
labelSize.height = otherDimension;
label.frame = (CGRect){ { 0, cumulativeLength }, labelSize };
cumulativeLength += CGRectGetHeight(label.frame);
break;
}
}
}
#pragma mark - Properties
- (UIView *)touchStatusView {
if (_touchStatusView) return _touchStatusView;
_touchStatusView = [[UIView alloc] initWithFrame:CGRectInset(self.bounds, 2, 2)];
_touchStatusView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
_touchStatusView.layer.cornerRadius = self.theDimension / 2;
_touchStatusView.layer.masksToBounds = YES;
return _touchStatusView;
}
- (void)setIndexTitles:(NSArray *)indexTitles {
if (_indexTitles == indexTitles) return;
_indexTitles = indexTitles;
[self.indexLabels makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self buildIndexLabels];
}
- (NSString *)currentIndexTitle {
return self.indexTitles[self.currentIndex];
}
- (void)setEndPadding:(CGFloat)endPadding {
if (_endPadding == endPadding) return;
_endPadding = endPadding;
[self.indexTitles makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self buildIndexLabels];
}
#pragma mark - Subviews
- (void)buildIndexLabels {
NSMutableArray *workingLabels = [NSMutableArray arrayWithCapacity:self.indexTitles.count];
NSUInteger tag = 0;
for (NSString *indexTitle in self.indexTitles) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = indexTitle;
label.tag = tag;
tag = tag + 1;
label.font = [UIFont boldSystemFontOfSize:12];
label.backgroundColor = _backgroundColor;
label.textColor = _labelColor;
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
[workingLabels addObject:label];
}
self.indexLabels = [NSArray arrayWithArray:workingLabels];
}
- (void)setNewIndexForPoint:(CGPoint)point {
for (UILabel *view in self.indexLabels) {
if (CGRectContainsPoint(view.frame, point)) {
NSUInteger newIndex = view.tag;
if (newIndex != _currentIndex) {
_currentIndex = newIndex;
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}
}
}
- (void)setBackgroundVisibility:(BOOL)flag {
CGFloat alpha = flag ? 0.25 : 0;
self.touchStatusView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
}
#pragma mark - Gestures
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
[self setNewIndexForPoint:touchPoint];
[self setBackgroundVisibility:YES];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
[self setNewIndexForPoint:touchPoint];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self setBackgroundVisibility:NO];
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
[self setBackgroundVisibility:!(recognizer.state == UIGestureRecognizerStateEnded)];
[self setNewIndexForPoint:[recognizer locationInView:self]];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
[self setBackgroundVisibility:!(recognizer.state == UIGestureRecognizerStateEnded)];
CGPoint translation = [recognizer locationInView:self];
[self setNewIndexForPoint:translation];
}
@end