forked from dimzzy/BaseAppKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BAToggleBar.m
444 lines (391 loc) · 13.3 KB
/
BAToggleBar.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
/*
Copyright 2011 Dmitry Stadnik. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY DMITRY STADNIK ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DMITRY STADNIK OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Dmitry Stadnik.
*/
#import "BAToggleBar.h"
#import "BAToggleItemLabel.h"
#define kToggleTailWidth 30
#define kToggleAnimationDuration 1
@interface BAToggleBar ()
@property(nonatomic, readonly) NSArray *itemViews;
@end
@interface BAToggleScrollView : UIScrollView {
}
@end
@implementation BAToggleScrollView
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
BAToggleBar *toggleBar = (BAToggleBar *)self.superview;
NSUInteger itemViewIndex = [toggleBar.itemViews indexOfObject:view];
if (itemViewIndex != NSNotFound) {
[toggleBar setSelectedItemIndex:itemViewIndex revealingItem:NO];
}
return YES;
}
@end
@implementation BAToggleBar
@synthesize centered = _centered;
@synthesize spacing = _spacing;
@synthesize itemViews = _itemViews;
@synthesize delegate = _delegate;
- (void)setupView {
_scrollView = [[BAToggleScrollView alloc] initWithFrame:self.bounds];
_scrollView.backgroundColor = [UIColor clearColor];
_scrollView.opaque = NO;
_scrollView.scrollsToTop = NO;
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
_scrollView.delegate = self;
[self addSubview:_scrollView];
_leftTailView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"togglebar-left.png"]];
_leftTailView.userInteractionEnabled = NO;
_leftTailView.opaque = NO;
[self addSubview:_leftTailView];
_leftTailState = BAToggleBarTailStateToggle;
_leftTailHidden = NO;
_rightTailView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"togglebar-right.png"]];
_rightTailView.userInteractionEnabled = NO;
_rightTailView.opaque = NO;
_rightTailView.backgroundColor = [UIColor clearColor];
[self addSubview:_rightTailView];
_rightTailState = BAToggleBarTailStateToggle;
_rightTailHidden = NO;
_itemViews = [[NSMutableArray alloc] init];
_items = [[NSMutableArray alloc] init];
}
- (id)initWithFrame:(CGRect)aRect {
if ((self = [super initWithFrame:aRect])) {
[self setupView];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self setupView];
}
- (void)dealloc {
[_scrollView release];
[_leftTailView release];
[_rightTailView release];
[_itemViews release];
[_items release];
[super dealloc];
}
- (UIImage *)leftTailImage {
return _leftTailView.image;
}
- (void)setLeftTailImage:(UIImage *)image {
_leftTailView.image = image;
}
- (UIImage *)rightTailImage {
return _rightTailView.image;
}
- (void)setRightTailImage:(UIImage *)image {
_rightTailView.image = image;
}
- (void)updateLeftTailView:(BOOL)animated {
BOOL hideLeft = (_scrollView.contentSize.width <= _scrollView.bounds.size.width ||
_scrollView.contentOffset.x <= 0);
switch (self.leftTailState) {
// case BAToggleBarTailStateToggleAnimated: {
// _leftTailView.hidden = NO;
// if (_leftTailHidden && !hideLeft) {
// _leftTailHidden = NO;
// if (animated) {
// [UIView animateWithDuration:kToggleAnimationDuration
// delay:0
// options:UIViewAnimationOptionCurveLinear
// animations:^(void) {
// CGRect r = _leftTailView.frame;
// r.origin.x += kToggleTailWidth;
// _leftTailView.frame = r;
// }
// completion:^(BOOL finished) {}];
// } else {
// CGRect r = _leftTailView.frame;
// r.origin.x += kToggleTailWidth;
// _leftTailView.frame = r;
// }
// } else if (!_leftTailHidden && hideLeft) {
// _leftTailHidden = YES;
// if (animated) {
// [UIView animateWithDuration:kToggleAnimationDuration
// delay:0
// options:UIViewAnimationOptionCurveLinear
// animations:^(void) {
// CGRect r = _leftTailView.frame;
// r.origin.x -= kToggleTailWidth;
// _leftTailView.frame = r;
// }
// completion:^(BOOL finished) {}];
// } else {
// CGRect r = _leftTailView.frame;
// r.origin.x -= kToggleTailWidth;
// _leftTailView.frame = r;
// }
// }
// break;
// }
case BAToggleBarTailStateToggle: {
_leftTailView.hidden = hideLeft;
_leftTailHidden = hideLeft;
break;
}
case BAToggleBarTailStateHidden: {
_leftTailView.hidden = YES;
_leftTailHidden = YES;
break;
}
case BAToggleBarTailStateVisible: {
_leftTailView.hidden = NO;
_leftTailHidden = NO;
break;
}
}
}
- (void)updateRightTailView:(BOOL)animated {
BOOL hideRight = (_scrollView.contentSize.width <= _scrollView.bounds.size.width ||
_scrollView.contentOffset.x + _scrollView.bounds.size.width >= _scrollView.contentSize.width);
switch (self.rightTailState) {
// case BAToggleBarTailStateToggleAnimated: {
// _rightTailView.hidden = NO;
// if (_rightTailHidden && !hideRight) {
// _rightTailHidden = NO;
// if (animated) {
// [UIView animateWithDuration:kToggleAnimationDuration
// delay:0
// options:UIViewAnimationOptionCurveLinear
// animations:^(void) {
// CGRect r = _rightTailView.frame;
// r.origin.x -= kToggleTailWidth;
// _rightTailView.frame = r;
// }
// completion:^(BOOL finished) {}];
// } else {
// CGRect r = _rightTailView.frame;
// r.origin.x -= kToggleTailWidth;
// _rightTailView.frame = r;
// }
// } else if (!_rightTailHidden && hideRight) {
// _rightTailHidden = YES;
// if (animated) {
// [UIView animateWithDuration:kToggleAnimationDuration
// delay:0
// options:UIViewAnimationOptionCurveLinear
// animations:^(void) {
// CGRect r = _rightTailView.frame;
// r.origin.x += kToggleTailWidth;
// _rightTailView.frame = r;
// }
// completion:^(BOOL finished) {}];
// } else {
// CGRect r = _rightTailView.frame;
// r.origin.x += kToggleTailWidth;
// _rightTailView.frame = r;
// }
// }
// break;
// }
case BAToggleBarTailStateToggle: {
_rightTailView.hidden = hideRight;
_rightTailHidden = hideRight;
break;
}
case BAToggleBarTailStateHidden: {
_rightTailView.hidden = YES;
_rightTailHidden = YES;
break;
}
case BAToggleBarTailStateVisible: {
_rightTailView.hidden = NO;
_rightTailHidden = NO;
break;
}
}
}
- (void)updateTailViews:(BOOL)animated {
[self updateLeftTailView:animated];
[self updateRightTailView:animated];
}
- (BAToggleBarTailState)leftTailState {
return _leftTailState;
}
- (void)setLeftTailState:(BAToggleBarTailState)tailState {
if (_leftTailState == tailState) {
return;
}
_leftTailState = tailState;
[self updateTailViews:NO];
}
- (BAToggleBarTailState)rightTailState {
return _rightTailState;
}
- (void)setRightTailState:(BAToggleBarTailState)tailState {
if (_rightTailState == tailState) {
return;
}
_rightTailState = tailState;
[self updateTailViews:NO];
}
- (void)updateItemsState {
for (NSUInteger index = 0; index < [_itemViews count]; index++) {
UIView<BAToggleItem> *itemView = [_itemViews objectAtIndex:index];
itemView.selected = (index == _selectedItemIndex);
}
}
- (void)layoutItemViews {
const CGFloat width = self.bounds.size.width;
const CGFloat height = self.bounds.size.height;
CGFloat x = 0;
if (self.leftTailImage) {
x += kToggleTailWidth;
}
for (UIView *subview in _scrollView.subviews) {
if ([_itemViews indexOfObject:subview] != NSNotFound) {
// item view
CGSize itemSize = [subview sizeThatFits:CGSizeMake(CGFLOAT_MAX / 2, height)];
subview.frame = CGRectMake(x, 0, itemSize.width, height);
x += itemSize.width + self.spacing;
} else {
// separator view
subview.frame = CGRectMake(x, (height - subview.bounds.size.height) / 2,
subview.bounds.size.width, subview.bounds.size.height);
x += subview.bounds.size.width + self.spacing;
}
}
if (self.rightTailImage) {
x += kToggleTailWidth;
}
x -= self.spacing;
if (x < width && self.centered) {
const CGFloat offset = roundf((width - x) / 2);
x = width;
for (UIView *subview in _scrollView.subviews) {
CGRect subviewFrame = subview.frame;
subview.frame = CGRectMake(subviewFrame.origin.x + offset, subviewFrame.origin.y,
subviewFrame.size.width, subviewFrame.size.height);
}
}
[_scrollView setContentSize:CGSizeMake(x, height)];
[self updateTailViews:NO];
}
- (void)layoutSubviews {
[super layoutSubviews];
// position scroll view in a way to prevent bounce bug
if (!CGRectEqualToRect(self.bounds, _scrollView.frame)) {
_scrollView.frame = self.bounds;
}
CGRect leftFrame = CGRectMake(0, 0, kToggleTailWidth, self.bounds.size.height);
// if (_leftTailState == BAToggleBarTailStateToggleAnimated && _leftTailHidden) {
// leftFrame.origin.x -= kToggleTailWidth;
// }
_leftTailView.frame = leftFrame;
CGRect rightFrame = CGRectMake(self.bounds.size.width - kToggleTailWidth, 0,
kToggleTailWidth, self.bounds.size.height);
// if (_rightTailState == BAToggleBarTailStateToggleAnimated && _rightTailHidden) {
// rightFrame.origin.x += kToggleTailWidth;
// }
_rightTailView.frame = rightFrame;
[self updateTailViews:NO];
}
- (BAToggleItemLabel *)createDefaultItemView:(id)item {
BAToggleItemLabel *itemView = [[[BAToggleItemLabel alloc] initWithFrame:CGRectZero] autorelease];
itemView.text = [item description];
itemView.font = [UIFont boldSystemFontOfSize:14];
itemView.textColor = [UIColor whiteColor];
itemView.shadowColor = [UIColor blackColor];
itemView.shadowOffset = CGSizeMake(0, -1);
return itemView;
}
- (NSArray *)items {
return [NSArray arrayWithArray:_items];
}
- (void)setItems:(NSArray *)items {
[_items removeAllObjects];
if (items) {
[_items addObjectsFromArray:items];
}
NSArray *oldSubviews = [[NSArray alloc] initWithArray:_scrollView.subviews];
for (UIView *subiew in oldSubviews) {
[subiew removeFromSuperview];
}
[oldSubviews release];
[_itemViews removeAllObjects];
for (NSUInteger itemIndex = 0; itemIndex < [_items count]; itemIndex++) {
if (itemIndex > 0 && self.delegate && [self.delegate respondsToSelector:@selector(toggleBarSeparatorView:)]) {
UIView *separatorView = [self.delegate toggleBarSeparatorView:self];
if (separatorView) {
[_scrollView addSubview:separatorView];
}
}
id item = [_items objectAtIndex:itemIndex];
UIView<BAToggleItem> *itemView = nil;
if (self.delegate && [self.delegate respondsToSelector:@selector(toggleBar:viewForItem:atIndex:)]) {
itemView = [self.delegate toggleBar:self viewForItem:item atIndex:itemIndex];
}
if (!itemView) {
itemView = [self createDefaultItemView:item];
}
[_scrollView addSubview:itemView];
[_itemViews addObject:itemView];
}
[self updateItemsState];
[self layoutItemViews];
}
- (NSInteger)selectedItemIndex {
return _selectedItemIndex;
}
- (void)setSelectedItemIndex:(NSInteger)selectedItemIndex revealingItem:(BOOL)revealingItem {
if (selectedItemIndex < 0 || selectedItemIndex >= [_items count]) {
selectedItemIndex = -1;
}
if (_selectedItemIndex == selectedItemIndex) {
return;
}
_selectedItemIndex = selectedItemIndex;
[self updateItemsState];
if (revealingItem && _selectedItemIndex >= 0) {
UIView *view = [_itemViews objectAtIndex:_selectedItemIndex];
[_scrollView scrollRectToVisible:view.frame animated:YES];
}
if (self.delegate) {
id item = (selectedItemIndex < 0) ? nil : [_items objectAtIndex:_selectedItemIndex];
[self.delegate toggleBar:self didSelectItem:item atIndex:_selectedItemIndex];
}
}
- (void)setSelectedItemIndex:(NSInteger)selectedItemIndex {
[self setSelectedItemIndex:selectedItemIndex revealingItem:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// if (self.leftTailState != BAToggleBarTailStateToggleAnimated) {
// [self updateLeftTailView:YES];
// }
// if (self.rightTailState != BAToggleBarTailStateToggleAnimated) {
// [self updateRightTailView:YES];
// }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// if (self.leftTailState == BAToggleBarTailStateToggleAnimated) {
// [self updateLeftTailView:YES];
// }
// if (self.rightTailState == BAToggleBarTailStateToggleAnimated) {
// [self updateRightTailView:YES];
// }
}
@end