-
Notifications
You must be signed in to change notification settings - Fork 1
/
TLScrollingSegmentedControl.m
203 lines (162 loc) · 7.62 KB
/
TLScrollingSegmentedControl.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
//
// TLScrollingSegmentedControl.m
// TLCommon
//
// Created by Joshua Bleecher Snyder on 9/8/09.
//
#import "TLScrollingSegmentedControl.h"
#import "TLScrollingSegmentedControlDelegate.h"
#import "CGGeometry_TLCommon.h"
#define kFogWidth 25.0f
#define kRecommendedHeight 30.0f
#define kDefaultFogColor [UIColor colorWithWhite:0.1f alpha:0.8f]
#define ClampToUnit(f) MAX(0.0f, MIN(1.0f, f))
#pragma mark -
@interface TLScrollingSegmentedControl ()
- (void)updateFog;
- (void)segmentedControlValueChanged:(UISegmentedControl *)control;
- (void)setFogColor:(UIColor *)newFogColor;
@property(nonatomic, retain, readwrite) UIScrollView *scrollView;
@property(nonatomic, retain, readwrite) UISegmentedControl *segmentedControl;
@property(nonatomic, retain, readwrite) NSArray *segmentTitles;
@property(nonatomic, retain, readwrite) CAGradientLayer *leftFog;
@property(nonatomic, retain, readwrite) CAGradientLayer *rightFog;
@end
#pragma mark -
@implementation TLScrollingSegmentedControl
@synthesize scrollView;
@synthesize segmentedControl;
@synthesize segmentTitles;
@synthesize delegate;
@synthesize leftFog;
@synthesize rightFog;
+ (CGFloat)recommendedHeight {
return kRecommendedHeight;
}
- (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles {
if(self = [super initWithFrame:frame]) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.backgroundColor = [UIColor blackColor];
self.opaque = YES;
self.scrollView = [[[UIScrollView alloc] initWithFrame:CGRectZeroWithSize(frame.size)] autorelease];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
self.scrollView.canCancelContentTouches = YES;
self.scrollView.delaysContentTouches = YES;
self.scrollView.delegate = self;
[self addSubview:self.scrollView];
self.segmentTitles = titles;
self.segmentedControl = [[[UISegmentedControl alloc] initWithItems:self.segmentTitles] autorelease];
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
self.segmentedControl.frame = CGRectZeroWithWidthAndHeight(self.segmentedControl.frame.size.width, frame.size.height);
[self.segmentedControl addTarget:self
action:@selector(segmentedControlValueChanged:)
forControlEvents:UIControlEventValueChanged];
[self.segmentedControl setSelectedSegmentIndex:0];
self.scrollView.contentSize = self.segmentedControl.bounds.size;
[self.scrollView addSubview:self.segmentedControl];
self.leftFog = [CAGradientLayer layer];
self.leftFog.bounds = CGRectZeroWithWidthAndHeight(kFogWidth,
self.bounds.size.height);
self.leftFog.position = CGPointMake(kFogWidth / 2.0f,
self.bounds.size.height / 2.0f); // position == center!
self.leftFog.startPoint = CGPointMake(0.0f, 0.5f); // left middle
self.leftFog.endPoint = CGPointMake(1.0f, 0.5f); // right middle
self.leftFog.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
[self.layer addSublayer:self.leftFog];
self.rightFog = [CAGradientLayer layer];
self.rightFog.bounds = CGRectZeroWithWidthAndHeight(kFogWidth,
self.bounds.size.height);
self.rightFog.position = CGPointMake(self.bounds.size.width - kFogWidth / 2.0f,
self.bounds.size.height / 2.0f); // position == center!
self.rightFog.startPoint = CGPointMake(1.0f, 0.5f); // left middle
self.rightFog.endPoint = CGPointMake(0.0f, 0.5f); // right middle
self.rightFog.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
[self.layer addSublayer:self.rightFog];
[self setFogColor:kDefaultFogColor];
[self updateFog];
}
return self;
}
- (void)setFogColor:(UIColor *)newFogColor {
self.leftFog.colors = [NSArray arrayWithObjects:
(id)newFogColor.CGColor,
(id)[UIColor clearColor].CGColor,
nil];
self.rightFog.colors = [NSArray arrayWithObjects:
(id)newFogColor.CGColor,
(id)[UIColor clearColor].CGColor,
nil];
}
- (void)segmentedControlValueChanged:(UISegmentedControl *)control {
if([delegate respondsToSelector:@selector(scrollingSegmentedControl:didSelectTitle:)]) {
NSString *selectedTitle = [self.segmentedControl titleForSegmentAtIndex:self.segmentedControl.selectedSegmentIndex];
[[self retain] autorelease];
[delegate scrollingSegmentedControl:self didSelectTitle:selectedTitle];
}
}
- (void)setTintColor:(UIColor *)tintColor {
self.segmentedControl.tintColor = tintColor;
}
- (BOOL)setSelectedTitle:(NSString *)title animated:(BOOL)animated {
NSInteger titleIndex = [self.segmentTitles indexOfObject:title];
BOOL titleIndexFound = (titleIndex != NSNotFound);
if(titleIndexFound) {
self.segmentedControl.selectedSegmentIndex = titleIndex;
UISegmentedControl *sizer = [[[UISegmentedControl alloc] initWithItems:[self.segmentTitles subarrayWithRange:NSMakeRange(0, titleIndex)]]
autorelease];
sizer.segmentedControlStyle = UISegmentedControlStyleBar;
CGFloat desiredTitleOffset = sizer.bounds.size.width - self.scrollView.bounds.size.width / 2.0f;
CGFloat maximumContentOffset = self.scrollView.contentSize.width - self.scrollView.bounds.size.width;
CGFloat scrollToOffset = MAX(0.0f, MIN(maximumContentOffset, desiredTitleOffset));
[self.scrollView setContentOffset:CGPointMake(scrollToOffset, 0)
animated:animated];
}
return titleIndexFound;
}
- (NSString *)selectedTitle {
return [self.segmentTitles objectAtIndex:self.segmentedControl.selectedSegmentIndex];
}
- (void)dealloc {
delegate = nil;
[segmentedControl removeTarget:self action:NULL forControlEvents:UIControlEventAllEvents];
[segmentedControl release];
segmentedControl = nil;
scrollView.delegate = nil;
[scrollView release];
scrollView = nil;
[segmentTitles release];
segmentTitles = nil;
[leftFog release];
leftFog = nil;
[rightFog release];
rightFog = nil;
[super dealloc];
}
- (void)updateFog {
CGFloat leftEdgeDistance = self.scrollView.contentOffset.x;
CGFloat rightEdgeDistance = self.scrollView.contentSize.width - self.scrollView.contentOffset.x - self.scrollView.bounds.size.width;
CGFloat leftPercentFogDistance = ClampToUnit(1.0f - leftEdgeDistance / kFogWidth);
CGFloat rightPercentFogDistance = ClampToUnit(1.0f - rightEdgeDistance / kFogWidth);
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
self.leftFog.position = CGPointMake(kFogWidth / 2.0f - leftPercentFogDistance * kFogWidth,
self.bounds.size.height / 2.0f);
self.rightFog.position = CGPointMake(self.bounds.size.width - kFogWidth / 2.0f + rightPercentFogDistance * kFogWidth,
self.bounds.size.height / 2.0f);
[CATransaction commit];
}
#pragma mark -
#pragma mark UIScrollViewDelegate methods
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
[self updateFog];
}
@end