forked from xxhp/NGTabBarController
/
NGTabBar.m
371 lines (296 loc) · 12.8 KB
/
NGTabBar.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
#import "NGTabBar.h"
#import "NGTabBarItem.h"
#import <QuartzCore/QuartzCore.h>
#define kNGDefaultTintColor [UIColor blackColor]
#define kNGDefaultItemHighlightColor [UIColor colorWithWhite:1.f alpha:0.2f]
@interface NGTabBar () {
CGGradientRef _gradientRef;
}
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIView *itemHighlightView;
- (void)createGradient;
- (void)updateItemHighlight;
- (CGFloat)dimensionToBeConsideredOfItem:(NGTabBarItem *)item;
@end
@implementation NGTabBar
@synthesize items = _items;
@synthesize selectedItemIndex = _selectedItemIndex;
@synthesize position = _position;
@synthesize layoutStrategy = _layoutStrategy;
@synthesize itemPadding = _itemPadding;
@synthesize tintColor = _tintColor;
@synthesize backgroundImage = _backgroundImage;
@synthesize backgroundView = _backgroundView;
@synthesize itemHighlightView = _itemHighlightView;
@synthesize showsItemHighlight = _showsItemHighlight;
@synthesize itemHighlightColor = _itemHighlightColor;
////////////////////////////////////////////////////////////////////////
#pragma mark - Lifecycle
////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.alwaysBounceHorizontal = NO;
self.clipsToBounds = YES;
_selectedItemIndex = 0;
_layoutStrategy = NGTabBarLayoutStrategyStrungTogether;
_itemPadding = 0.f;
_position = kNGTabBarPositionDefault;
_showsItemHighlight = YES;
[self createGradient];
[self updateItemHighlight];
}
return self;
}
- (void)dealloc {
if (_gradientRef != NULL) {
CFRelease(_gradientRef);
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark - UIView
////////////////////////////////////////////////////////////////////////
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat currentFrameLeft = 0.f;
CGFloat currentFrameTop = 0.f;
CGFloat totalDimension = 0.f;
// we change item padding in strategy evenly distributed but don't want to change iVar
CGFloat appliedItemPadding = self.itemPadding;
// backgroundView gets same frame as tabBar
self.backgroundView.frame = self.bounds;
if (self.layoutStrategy == NGTabBarLayoutStrategyEvenlyDistributed || self.layoutStrategy == NGTabBarLayoutStrategyCentered) {
// compute total dimension needed
for (NGTabBarItem *item in self.items) {
totalDimension += [self dimensionToBeConsideredOfItem:item];
// we don't take padding only into account if we want to evenly distribute items
if (self.layoutStrategy != NGTabBarLayoutStrategyEvenlyDistributed) {
totalDimension += self.itemPadding;
}
}
// for evenly distributed items we calculate a new item padding
if (self.layoutStrategy == NGTabBarLayoutStrategyEvenlyDistributed) {
// the total padding needed for the whole tabBar
CGFloat totalPadding = NGTabBarIsVertical(self.position) ? self.bounds.size.height - totalDimension : self.bounds.size.width - totalDimension;
// we apply the padding (items.count - 1) times (always between two items)
if (self.items.count > 1) {
appliedItemPadding = MAX(0.f,totalPadding / (self.items.count - 1));
}
}
else if (self.layoutStrategy == NGTabBarLayoutStrategyCentered) {
// we only add padding between icons but we added it for each item in the loop above
totalDimension -= appliedItemPadding;
if (NGTabBarIsVertical(self.position)) {
currentFrameTop = floorf((self.bounds.size.height-totalDimension)/2.f);
} else {
currentFrameLeft = floorf((self.bounds.size.width-totalDimension)/2.f);
}
}
}
// re-position each item starting from current top/left
for (NGTabBarItem *item in self.items) {
CGRect frame = item.frame;
frame.origin.y = currentFrameTop;
frame.origin.x = currentFrameLeft;
item.frame = frame;
// move to next item position
if (NGTabBarIsVertical(self.position)) {
currentFrameTop += frame.size.height;
currentFrameTop += appliedItemPadding;
} else {
currentFrameLeft += frame.size.width;
currentFrameLeft += appliedItemPadding;
}
}
// re-compute content size
NGTabBarItem *lastItem = [self.items lastObject];
if (NGTabBarIsVertical(self.position)) {
self.contentSize = CGSizeMake(lastItem.frame.size.width, lastItem.frame.origin.y + lastItem.frame.size.height);
} else {
self.contentSize = CGSizeMake(lastItem.frame.origin.x + lastItem.frame.size.width, lastItem.frame.size.height);
}
[self updateItemHighlight];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
if (self.backgroundImage == nil) {
CGRect bounds = self.bounds;
CGPoint start;
CGPoint end;
// draw gradient
CGContextSaveGState(context);
CGContextClipToRect(context, bounds);
if (self.position == NGTabBarPositionBottom) {
start = CGPointMake(bounds.origin.x, bounds.origin.y);
end = CGPointMake(bounds.origin.x, bounds.origin.y + bounds.size.height);
} else if (self.position == NGTabBarPositionTop) {
start = CGPointMake(bounds.origin.x, bounds.origin.y + bounds.size.height);
end = CGPointMake(bounds.origin.x, bounds.origin.y);
} else if (self.position == NGTabBarPositionLeft) {
start = CGPointMake(bounds.origin.x + bounds.size.width, bounds.origin.y);
end = CGPointMake(bounds.origin.x, bounds.origin.y);
} else if (self.position == NGTabBarPositionRight) {
start = CGPointMake(bounds.origin.x, bounds.origin.y);
end = CGPointMake(bounds.origin.x + bounds.size.width, bounds.origin.y);
}
CGContextDrawLinearGradient(context, _gradientRef, start, end, 0);
CGContextRestoreGState(context);
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark - NGTabBar
////////////////////////////////////////////////////////////////////////
- (void)setItems:(NSArray *)items {
if (items != _items) {
[_items performSelector:@selector(removeFromSuperview)];
_items = items;
for (NGTabBarItem *item in _items) {
[self addSubview:item];
}
[self setNeedsLayout];
}
}
- (void)setPosition:(NGTabBarPosition)position {
if (position != _position) {
_position = position;
if (NGTabBarIsVertical(position)) {
self.alwaysBounceVertical = YES;
} else {
self.alwaysBounceVertical = NO;
}
[self setNeedsLayout];
[self setNeedsDisplay];
}
}
- (void)selectItemAtIndex:(NSUInteger)index {
[self deselectSelectedItem];
if (index < self.items.count) {
NGTabBarItem *item = [self.items objectAtIndex:index];
item.selected = YES;
self.selectedItemIndex = index;
[self updateItemHighlight];
}
}
- (void)deselectSelectedItem {
if (self.selectedItemIndex < self.items.count) {
NGTabBarItem *selectedItem = [self.items objectAtIndex:self.selectedItemIndex];
selectedItem.selected = NO;
self.selectedItemIndex = NSNotFound;
[self updateItemHighlight];
}
}
- (void)setBackgroundImage:(UIImage *)backgroundImage {
if (backgroundImage != _backgroundImage) {
[self.backgroundView removeFromSuperview];
_backgroundImage = backgroundImage;
if (backgroundImage != nil) {
// is the image a non-resizable image?
if (UIEdgeInsetsEqualToEdgeInsets(backgroundImage.capInsets,UIEdgeInsetsZero)) {
self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
self.backgroundView.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
[self insertSubview:self.backgroundView atIndex:0];
} else {
self.backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
}
} else {
self.backgroundView = nil;
}
}
}
- (UIColor *)tintColor {
return _tintColor ?: kNGDefaultTintColor;
}
- (void)setTintColor:(UIColor *)tintColor {
if (tintColor != _tintColor) {
_tintColor = tintColor;
[self createGradient];
[self setNeedsDisplay];
}
}
- (UIColor *)itemHighlightColor {
return _itemHighlightColor ?: kNGDefaultItemHighlightColor;
}
- (void)setItemHighlightColor:(UIColor *)itemHighlightColor {
if (itemHighlightColor != _itemHighlightColor) {
_itemHighlightColor = itemHighlightColor;
[self updateItemHighlight];
}
}
- (void)setShowsItemHighlight:(BOOL)showsItemHighlight {
if (showsItemHighlight != _showsItemHighlight) {
_showsItemHighlight = showsItemHighlight;
[self updateItemHighlight];
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark - Private
////////////////////////////////////////////////////////////////////////
- (CGFloat)dimensionToBeConsideredOfItem:(NGTabBarItem *)item {
if (NGTabBarIsVertical(self.position)) {
return item.frame.size.height;
} else {
return item.frame.size.width;
}
}
- (void)createGradient {
if (_gradientRef != NULL) {
CFRelease(_gradientRef);
}
UIColor *baseColor = self.tintColor;
CGFloat hue, saturation, brightness, alpha;
NSArray *colors = nil;
// TODO: This is a temporary workaround because getHue:saturation:brightness:alpha: is iOS 5 and up
// We need a better way of drawing a TabBar-Gradient
if ([baseColor respondsToSelector:@selector(getHue:saturation:brightness:alpha:)]) {
[baseColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
colors = [NSArray arrayWithObjects:
[UIColor colorWithHue:hue saturation:saturation brightness:brightness+0.2 alpha:alpha],
[UIColor colorWithHue:hue saturation:saturation brightness:brightness+0.15 alpha:alpha],
[UIColor colorWithHue:hue saturation:saturation brightness:brightness+0.1 alpha:alpha],
baseColor, nil];
} else {
colors = [NSArray arrayWithObjects:
baseColor,
baseColor,
baseColor,
baseColor, nil];
}
NSUInteger colorsCount = colors.count;
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors objectAtIndex:0] CGColor]);
NSArray *locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.25],
[NSNumber numberWithFloat:0.49],
[NSNumber numberWithFloat:0.5], nil];
CGFloat *gradientLocations = NULL;
NSUInteger locationsCount = locations.count;
gradientLocations = (CGFloat *)malloc(sizeof(CGFloat) * locationsCount);
for (NSUInteger i = 0; i < locationsCount; i++) {
gradientLocations[i] = [[locations objectAtIndex:i] floatValue];
}
NSMutableArray *gradientColors = [[NSMutableArray alloc] initWithCapacity:colorsCount];
[colors enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
[gradientColors addObject:(id)[(UIColor *)object CGColor]];
}];
_gradientRef = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
if (gradientLocations) {
free(gradientLocations);
}
}
- (void)updateItemHighlight {
if (self.selectedItemIndex != NSNotFound) {
CGRect itemRect = [[self.items objectAtIndex:self.selectedItemIndex] frame];
if (_itemHighlightView == nil) {
self.itemHighlightView = [[UIView alloc] initWithFrame:CGRectZero];
self.itemHighlightView.layer.cornerRadius = 5.f;
[self addSubview:self.itemHighlightView];
}
self.itemHighlightView.backgroundColor = self.itemHighlightColor;
self.itemHighlightView.frame = NGTabBarIsVertical(self.position) ? CGRectInset(itemRect, 2.f, 0.f) : CGRectInset(itemRect, 0.f, 2.f);
self.itemHighlightView.hidden = !self.showsItemHighlight;
} else {
self.itemHighlightView.hidden = YES;
}
}
@end