forked from veader/V8HorizontalPickerView
-
Notifications
You must be signed in to change notification settings - Fork 1
/
V8HorizontalPickerView.m
683 lines (568 loc) · 20.9 KB
/
V8HorizontalPickerView.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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//
// V8HorizontalPickerView.m
//
// Created by Shawn Veader on 9/17/10.
// Copyright 2010 V8 Labs, LLC. All rights reserved.
//
#import "V8HorizontalPickerView.h"
#pragma mark - Internal Method Interface
@interface V8HorizontalPickerView (InternalMethods)
- (void)collectData;
- (void)getNumberOfElementsFromDataSource;
- (void)getElementWidthsFromDelegate;
- (void)setTotalWidthOfScrollContent;
- (void)updateScrollContentInset;
- (void)addScrollView;
- (void)drawPositionIndicator;
- (V8HorizontalPickerLabel *)labelForForElementAtIndex:(NSInteger)index withTitle:(NSString *)title;
- (CGRect)frameForElementAtIndex:(NSInteger)index;
- (CGRect)frameForLeftScrollEdgeView;
- (CGRect)frameForRightScrollEdgeView;
- (CGFloat)leftScrollEdgeWidth;
- (CGFloat)rightScrollEdgeWidth;
- (CGPoint)currentCenter;
- (void)scrollToElementNearestToCenter;
- (NSInteger)nearestElementToCenter;
- (NSInteger)nearestElementToPoint:(CGPoint)point;
- (NSInteger)elementContainingPoint:(CGPoint)point;
- (NSInteger)offsetForElementAtIndex:(NSInteger)index;
- (NSInteger)centerOfElementAtIndex:(NSInteger)index;
- (void)scrollViewTapped:(UITapGestureRecognizer *)recognizer;
- (NSInteger)tagForElementAtIndex:(NSInteger)index;
- (NSInteger)indexForElement:(UIView *)element;
@end
#pragma mark - Implementation
@implementation V8HorizontalPickerView : UIView
@synthesize dataSource, delegate;
@synthesize numberOfElements, currentSelectedIndex; // readonly
@synthesize elementFont, textColor, selectedTextColor;
@synthesize selectionPoint, selectionIndicatorView, indicatorPosition;
@synthesize leftEdgeView, rightEdgeView;
@synthesize leftScrollEdgeView, rightScrollEdgeView, scrollEdgeViewPadding;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
elementWidths = [NSMutableArray array];
[self addScrollView];
self.textColor = [UIColor blackColor];
self.elementFont = [UIFont systemFontOfSize:12.0f];
numberOfElements = 0;
elementPadding = 0;
currentSelectedIndex = 0;
dataHasBeenLoaded = NO;
scrollSizeHasBeenSet = NO;
// default to the center
selectionPoint = CGPointMake(frame.size.width / 2, 0.0f);
indicatorPosition = V8HorizontalPickerIndicatorBottom;
firstVisibleElement = -1;
lastVisibleElement = -1;
scrollEdgeViewPadding = 0.0f;
self.autoresizesSubviews = YES;
self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
return self;
}
#pragma mark - LayoutSubViews
- (void)layoutSubviews {
[super layoutSubviews];
if (!dataHasBeenLoaded) {
[self collectData];
}
if (!scrollSizeHasBeenSet) {
[self setTotalWidthOfScrollContent];
}
SEL titleForElementSelector = @selector(horizontalPickerView:titleForElementAtIndex:);
SEL viewForElementSelector = @selector(horizontalPickerView:viewForElementAtIndex:);
SEL setSelectedSelector = @selector(setSelectedElement:);
CGRect visibleBounds = [self bounds];
CGRect scaledViewFrame = CGRectZero;
// remove any subviews that are no longer visible
for (UIView *view in [_scrollView subviews]) {
scaledViewFrame = [_scrollView convertRect:[view frame] toView:self];
// if the view doesn't intersect, it's not visible, so we can recycle it
if (!CGRectIntersectsRect(scaledViewFrame, visibleBounds)) {
[view removeFromSuperview];
} else { // if it is still visible, update it's selected state
if ([view respondsToSelector:setSelectedSelector]) {
// view's tag is it's index
BOOL isSelected = (currentSelectedIndex == [self indexForElement:view]);
[(V8HorizontalPickerLabel *)view setSelectedElement:isSelected];
}
}
}
// find needed elements by looking at left and right edges of frame
CGPoint offset = _scrollView.contentOffset;
int firstNeededElement = [self nearestElementToPoint:CGPointMake(offset.x, 0.0f)];
int lastNeededElement = [self nearestElementToPoint:CGPointMake(offset.x + visibleBounds.size.width, 0.0f)];
// add any views that have become visible
UIView *view = nil;
for (int i = firstNeededElement; i <= lastNeededElement; i++) {
view = nil; // paranoia
view = [_scrollView viewWithTag:[self tagForElementAtIndex:i]];
if (!view) {
if (i < numberOfElements) { // make sure we are not requesting data out of range
if (self.delegate && [self.delegate respondsToSelector:titleForElementSelector]) {
NSString *title = [self.delegate horizontalPickerView:self titleForElementAtIndex:i];
view = [self labelForForElementAtIndex:i withTitle:title];
} else if (self.delegate && [self.delegate respondsToSelector:viewForElementSelector]) {
view = [self.delegate horizontalPickerView:self viewForElementAtIndex:i];
}
if (view) {
// use the index as the tag so we can find it later
view.tag = [self tagForElementAtIndex:i];
[_scrollView addSubview:view];
}
}
}
}
// add the left or right edge views if visible
CGRect viewFrame = CGRectZero;
if (leftScrollEdgeView) {
viewFrame = [self frameForLeftScrollEdgeView];
scaledViewFrame = [_scrollView convertRect:viewFrame toView:self];
if (CGRectIntersectsRect(scaledViewFrame, visibleBounds) && ![leftScrollEdgeView isDescendantOfView:_scrollView]) {
leftScrollEdgeView.frame = viewFrame;
[_scrollView addSubview:leftScrollEdgeView];
}
}
if (rightScrollEdgeView) {
viewFrame = [self frameForRightScrollEdgeView];
scaledViewFrame = [_scrollView convertRect:viewFrame toView:self];
if (CGRectIntersectsRect(scaledViewFrame, visibleBounds) && ![rightScrollEdgeView isDescendantOfView:_scrollView]) {
rightScrollEdgeView.frame = viewFrame;
[_scrollView addSubview:rightScrollEdgeView];
}
}
// save off what's visible now
firstVisibleElement = firstNeededElement;
lastVisibleElement = lastNeededElement;
}
#pragma mark - Getters and Setters
- (void)setDelegate:(id)newDelegate {
if (delegate != newDelegate) {
delegate = newDelegate;
[self collectData];
}
}
- (void)setDataSource:(id)newDataSource {
if (dataSource != newDataSource) {
dataSource = newDataSource;
[self collectData];
}
}
- (void)setSelectionPoint:(CGPoint)point {
if (!CGPointEqualToPoint(point, selectionPoint)) {
selectionPoint = point;
[self updateScrollContentInset];
}
}
// allow the setting of this views background color to change the scroll view
- (void)setBackgroundColor:(UIColor *)newColor {
[super setBackgroundColor:newColor];
_scrollView.backgroundColor = newColor;
// TODO: set all subviews as well?
}
- (void)setIndicatorPosition:(V8HorizontalPickerIndicatorPosition)position {
if (indicatorPosition != position) {
indicatorPosition = position;
[self drawPositionIndicator];
}
}
- (void)setSelectionIndicatorView:(UIView *)indicatorView {
if (selectionIndicatorView != indicatorView) {
if (selectionIndicatorView) {
[selectionIndicatorView removeFromSuperview];
}
selectionIndicatorView = indicatorView;
[self drawPositionIndicator];
}
}
- (void)setLeftEdgeView:(UIView *)leftView {
if (leftEdgeView != leftView) {
if (leftEdgeView) {
[leftEdgeView removeFromSuperview];
}
leftEdgeView = leftView;
CGRect tmpFrame = leftEdgeView.frame;
tmpFrame.origin.x = 0.0f;
tmpFrame.origin.y = 0.0f;
leftEdgeView.frame = tmpFrame;
[self addSubview:leftEdgeView];
}
}
- (void)setRightEdgeView:(UIView *)rightView {
if (rightEdgeView != rightView) {
if (rightEdgeView) {
[rightEdgeView removeFromSuperview];
}
rightEdgeView = rightView;
CGRect tmpFrame = rightEdgeView.frame;
tmpFrame.origin.x = self.frame.size.width - tmpFrame.size.width;
tmpFrame.origin.y = 0.0f;
rightEdgeView.frame = tmpFrame;
[self addSubview:rightEdgeView];
}
}
- (void)setLeftScrollEdgeView:(UIView *)leftView {
if (leftScrollEdgeView != leftView) {
if (leftScrollEdgeView) {
[leftScrollEdgeView removeFromSuperview];
}
leftScrollEdgeView = leftView;
scrollSizeHasBeenSet = NO;
[self updateScrollContentInset];
[self setNeedsLayout];
}
}
- (void)setRightScrollEdgeView:(UIView *)rightView {
if (rightScrollEdgeView != rightView) {
if (rightScrollEdgeView) {
[rightScrollEdgeView removeFromSuperview];
}
rightScrollEdgeView = rightView;
scrollSizeHasBeenSet = NO;
[self updateScrollContentInset];
[self setNeedsLayout];
}
}
#pragma mark - Reload Data Method
- (void)reloadData {
// remove all scrollview subviews and "recycle" them
for (UIView *view in [_scrollView subviews]) {
[view removeFromSuperview];
}
firstVisibleElement = NSIntegerMax;
lastVisibleElement = NSIntegerMin;
[self collectData];
[self setNeedsLayout];
}
- (void)collectData {
scrollSizeHasBeenSet = NO;
dataHasBeenLoaded = NO;
[self getNumberOfElementsFromDataSource];
[self getElementWidthsFromDelegate];
[self setTotalWidthOfScrollContent];
[self updateScrollContentInset];
dataHasBeenLoaded = YES;
}
#pragma mark - Scroll To Element Method
- (void)scrollToElement:(NSInteger)index animated:(BOOL)animate {
int x = [self centerOfElementAtIndex:index] - selectionPoint.x;
[_scrollView setContentOffset:CGPointMake(x, 0) animated:animate];
currentSelectedIndex = index;
// notify delegate of the selected index
SEL delegateCall = @selector(horizontalPickerView:didSelectElementAtIndex:);
if (self.delegate && [self.delegate respondsToSelector:delegateCall]) {
[self.delegate horizontalPickerView:self didSelectElementAtIndex:index];
}
#if (__IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_4_3)
[self setNeedsLayout];
#endif
}
#pragma mark - UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// set the current item under the center to "highlighted" or current
currentSelectedIndex = [self nearestElementToCenter];
#if (__IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_4_3)
[self setNeedsLayout];
#endif
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
// only do this if we aren't decelerating
if (!decelerate) {
[self scrollToElementNearestToCenter];
}
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
// [self scrollToElementNearestToCenter];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self scrollToElementNearestToCenter];
}
#pragma mark - View Creation Methods (Internal Methods)
- (void)addScrollView {
if (_scrollView == nil) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.delegate = self;
_scrollView.scrollEnabled = YES;
_scrollView.scrollsToTop = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.bouncesZoom = NO;
_scrollView.alwaysBounceHorizontal = YES;
_scrollView.alwaysBounceVertical = NO;
_scrollView.minimumZoomScale = 1.0; // setting min/max the same disables zooming
_scrollView.maximumZoomScale = 1.0;
_scrollView.contentInset = UIEdgeInsetsZero;
_scrollView.decelerationRate = 0.1; //UIScrollViewDecelerationRateNormal;
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_scrollView.autoresizesSubviews = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)];
[_scrollView addGestureRecognizer:tapRecognizer];
[self addSubview:_scrollView];
}
}
- (void)drawPositionIndicator {
CGFloat x = self.selectionPoint.x - (selectionIndicatorView.frame.size.width / 2);
CGFloat y;
switch (self.indicatorPosition) {
case V8HorizontalPickerIndicatorTop: {
y = 0.0f;
break;
}
case V8HorizontalPickerIndicatorBottom: {
y = self.frame.size.height - selectionIndicatorView.frame.size.height;
break;
}
default:
break;
}
// properly place indicator image in view relative to selection point
CGRect tmpFrame = CGRectMake(x, y,
selectionIndicatorView.frame.size.width,
selectionIndicatorView.frame.size.height);
selectionIndicatorView.frame = tmpFrame;
[self addSubview:selectionIndicatorView];
}
// create a UILabel for this element.
- (V8HorizontalPickerLabel *)labelForForElementAtIndex:(NSInteger)index withTitle:(NSString *)title {
CGRect labelFrame = [self frameForElementAtIndex:index];
V8HorizontalPickerLabel *elementLabel = [[V8HorizontalPickerLabel alloc] initWithFrame:labelFrame];
elementLabel.textAlignment = UITextAlignmentCenter;
elementLabel.backgroundColor = self.backgroundColor;
elementLabel.text = title;
elementLabel.font = self.elementFont;
elementLabel.normalStateColor = self.textColor;
elementLabel.selectedStateColor = self.selectedTextColor;
elementLabel.selectedElement = (currentSelectedIndex == index);
return elementLabel;
}
#pragma mark - DataSource Calling Method (Internal Method)
- (void)getNumberOfElementsFromDataSource {
SEL dataSourceCall = @selector(numberOfElementsInHorizontalPickerView:);
if (self.dataSource && [self.dataSource respondsToSelector:dataSourceCall]) {
numberOfElements = [self.dataSource numberOfElementsInHorizontalPickerView:self];
} else {
numberOfElements = 0;
}
}
#pragma mark - Delegate Calling Method (Internal Method)
- (void)getElementWidthsFromDelegate {
SEL delegateCall = @selector(horizontalPickerView:widthForElementAtIndex:);
[elementWidths removeAllObjects];
for (int i = 0; i < numberOfElements; i++) {
if (self.delegate && [self.delegate respondsToSelector:delegateCall]) {
NSInteger width = [self.delegate horizontalPickerView:self widthForElementAtIndex:i];
[elementWidths addObject:[NSNumber numberWithInteger:width]];
}
}
}
#pragma mark - View Calculation and Manipulation Methods (Internal Methods)
// what is the total width of the content area?
- (void)setTotalWidthOfScrollContent {
NSInteger totalWidth = 0;
totalWidth += [self leftScrollEdgeWidth];
totalWidth += [self rightScrollEdgeWidth];
// sum the width of all elements
for (NSNumber *width in elementWidths) {
totalWidth += [width intValue];
totalWidth += elementPadding;
}
// TODO: is this necessary?
totalWidth -= elementPadding; // we add "one too many" in for loop
if (_scrollView) {
// create our scroll view as wide as all the elements to be included
_scrollView.contentSize = CGSizeMake(totalWidth, self.bounds.size.height);
scrollSizeHasBeenSet = YES;
}
}
// reset the content inset of the scroll view based on centering first and last elements.
- (void)updateScrollContentInset {
// update content inset if we have element widths
if ([elementWidths count] != 0) {
CGFloat scrollerWidth = _scrollView.frame.size.width;
CGFloat halfFirstWidth = 0.0f;
CGFloat halfLastWidth = 0.0f;
if ( [elementWidths count] > 0 ) {
halfFirstWidth = [[elementWidths objectAtIndex:0] floatValue] / 2.0;
halfLastWidth = [[elementWidths lastObject] floatValue] / 2.0;
}
// calculating the inset so that the bouncing on the ends happens more smooothly
// - first inset is the distance from the left edge to the left edge of the
// first element when that element is centered under the selection point.
// - represented below as the # area
// - last inset is the distance from the right edge to the right edge of
// the last element when that element is centered under the selection point.
// - represented below as the * area
//
// Selection
// +---------|---------------+
// |####| Element |**********| << UIScrollView
// +-------------------------+
CGFloat firstInset = selectionPoint.x - halfFirstWidth;
firstInset -= [self leftScrollEdgeWidth];
CGFloat lastInset = (scrollerWidth - selectionPoint.x) - halfLastWidth;
lastInset -= [self rightScrollEdgeWidth];
_scrollView.contentInset = UIEdgeInsetsMake(0, firstInset, 0, lastInset);
}
}
// what is the left-most edge of the element at the given index?
- (NSInteger)offsetForElementAtIndex:(NSInteger)index {
NSInteger offset = 0;
if (index >= [elementWidths count]) {
return 0;
}
offset += [self leftScrollEdgeWidth];
for (int i = 0; i < index && i < [elementWidths count]; i++) {
offset += [[elementWidths objectAtIndex:i] intValue];
offset += elementPadding;
}
return offset;
}
// return the tag for an element at a given index
- (NSInteger)tagForElementAtIndex:(NSInteger)index {
return (index + 1) * 10;
}
// return the index given an element's tag
- (NSInteger)indexForElement:(UIView *)element {
return (element.tag / 10) - 1;
}
// what is the center of the element at the given index?
- (NSInteger)centerOfElementAtIndex:(NSInteger)index {
if (index >= [elementWidths count]) {
return 0;
}
NSInteger elementOffset = [self offsetForElementAtIndex:index];
NSInteger elementWidth = [[elementWidths objectAtIndex:index] intValue] / 2;
return elementOffset + elementWidth;
}
// what is the frame for the element at the given index?
- (CGRect)frameForElementAtIndex:(NSInteger)index {
CGFloat width = 0.0f;
if ([elementWidths count] > index) {
width = [[elementWidths objectAtIndex:index] intValue];
}
return CGRectMake([self offsetForElementAtIndex:index], 0.0f, width, self.frame.size.height);
}
// what is the frame for the left scroll edge view?
- (CGRect)frameForLeftScrollEdgeView {
if (leftScrollEdgeView) {
CGFloat scrollHeight = _scrollView.contentSize.height;
CGFloat viewHeight = leftScrollEdgeView.frame.size.height;
return CGRectMake(0.0f, ((scrollHeight / 2.0f) - (viewHeight / 2.0f)),
leftScrollEdgeView.frame.size.width, viewHeight);
} else {
return CGRectZero;
}
}
// what is the width of the left edge of the scroll area?
- (CGFloat)leftScrollEdgeWidth {
if (leftScrollEdgeView) {
CGFloat width = leftScrollEdgeView.frame.size.width;
width += scrollEdgeViewPadding;
return width;
}
return 0.0f;
}
// what is the frame for the right scroll edge view?
- (CGRect)frameForRightScrollEdgeView {
if (rightScrollEdgeView) {
CGFloat scrollWidth = _scrollView.contentSize.width;
CGFloat scrollHeight = _scrollView.contentSize.height;
CGFloat viewWidth = rightScrollEdgeView.frame.size.width;
CGFloat viewHeight = rightScrollEdgeView.frame.size.height;
return CGRectMake(scrollWidth - viewWidth, ((scrollHeight / 2.0f) - (viewHeight / 2.0f)),
viewWidth, viewHeight);
} else {
return CGRectZero;
}
}
// what is the width of the right edge of the scroll area?
- (CGFloat)rightScrollEdgeWidth {
if (rightScrollEdgeView) {
CGFloat width = rightScrollEdgeView.frame.size.width;
width += scrollEdgeViewPadding;
return width;
}
return 0.0f;
}
// what is the "center", relative to the content offset and adjusted to selection point?
- (CGPoint)currentCenter {
CGFloat x = _scrollView.contentOffset.x + selectionPoint.x;
return CGPointMake(x, 0.0f);
}
// what is the element nearest to the center of the view?
- (NSInteger)nearestElementToCenter {
return [self nearestElementToPoint:[self currentCenter]];
}
// what is the element nearest to the given point?
- (NSInteger)nearestElementToPoint:(CGPoint)point {
for (int i = 0; i < numberOfElements; i++) {
CGRect frame = [self frameForElementAtIndex:i];
if (CGRectContainsPoint(frame, point)) {
return i;
} else if (point.x < frame.origin.x) {
// if the center is before this element, go back to last one,
// unless we're at the beginning
if (i > 0) {
return i - 1;
} else {
return 0;
}
break;
} else if (point.x > frame.origin.y) {
// if the center is past the last element, scroll to it
if (i == numberOfElements - 1) {
return i;
}
}
}
return 0;
}
// similar to nearestElementToPoint: however, this method does not look past beginning/end
- (NSInteger)elementContainingPoint:(CGPoint)point {
for (int i = 0; i < numberOfElements; i++) {
CGRect frame = [self frameForElementAtIndex:i];
if (CGRectContainsPoint(frame, point)) {
return i;
}
}
return -1;
}
// move scroll view to position nearest element under the center
- (void)scrollToElementNearestToCenter {
[self scrollToElement:[self nearestElementToCenter] animated:YES];
}
// use the gesture recognizer to slide to element under tap
- (void)scrollViewTapped:(UITapGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateRecognized ) {
CGPoint tapLocation = [recognizer locationInView:_scrollView];
NSInteger elementIndex = [self elementContainingPoint:tapLocation];
if (elementIndex != -1) { // point not in element
[self scrollToElement:elementIndex animated:YES];
}
}
}
@end
// ------------------------------------------------------------------------
#pragma mark - Picker Label Implementation
@implementation V8HorizontalPickerLabel : UILabel
@synthesize selectedElement, selectedStateColor, normalStateColor;
- (void)setSelectedElement:(BOOL)selected {
if (selectedElement != selected) {
if (selected) {
self.textColor = self.selectedStateColor;
} else {
self.textColor = self.normalStateColor;
}
selectedElement = selected;
[self setNeedsLayout];
}
}
- (void)setNormalStateColor:(UIColor *)color {
if (normalStateColor != color) {
normalStateColor = color;
self.textColor = normalStateColor;
[self setNeedsLayout];
}
}
@end