forked from iosdeveloper/InfiniTabBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfiniTabBar.m
404 lines (291 loc) · 11.1 KB
/
InfiniTabBar.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
//
// InfiniTabBar.m
// Created by http://github.com/iosdeveloper
//
#import "InfiniTabBar.h"
@implementation InfiniTabBar
@synthesize infiniTabBarDelegate;
@synthesize tabBars;
@synthesize aTabBar;
@synthesize bTabBar;
@synthesize arrow;
-(UIImage*)makeTriangle:(CGRect)rect where:(int)where{
CGSize size = CGSizeMake(rect.size.width, rect.size.height);
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
switch(where){
case 0:
//nop
break;
case 1:
//bot
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx,0.0,0.0);
CGContextAddLineToPoint(ctx,size.width/2.0,size.height);
CGContextAddLineToPoint(ctx,size.width,0.0);
CGContextClosePath(ctx);
break;
case 2:
//right
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx,0.0,size.height);
CGContextAddLineToPoint(ctx,size.width,size.height/2.0);
CGContextAddLineToPoint(ctx,0.0,0.0);
CGContextClosePath(ctx);
break;
case 4:
//up
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx,0.0,size.height);
CGContextAddLineToPoint(ctx,size.width/2.0,0.0);
CGContextAddLineToPoint(ctx,size.width,size.height);
CGContextClosePath(ctx);
break;
case 8:
//left
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx,0.0,size.height/2.0);
CGContextAddLineToPoint(ctx,size.width,size.height);
CGContextAddLineToPoint(ctx,size.width,0.0);
CGContextClosePath(ctx);
break;
}
CGContextSetStrokeColorWithColor(ctx, [[UIColor darkGrayColor] CGColor]);
CGContextSetLineWidth(ctx, 2.0);
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor darkGrayColor] CGColor]));
CGContextStrokePath(ctx);
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
- (void)didMoveToSuperview{
[super didMoveToSuperview];
[self arrowDecoration];
[self.superview addSubview:self.arrow];
}
-(void)arrowDecoration{
int position = [self currentTabBarTag];
UIScreen *screen = [UIScreen mainScreen];
CGRect sbounds = screen.applicationFrame;
if (imageviewleft){
[imageviewleft removeFromSuperview];
[imageviewleft release];
imageviewleft=NULL;
}
if (imageviewright){
[imageviewright removeFromSuperview];
[imageviewright release];
imageviewright=NULL;
}
if ((self.tabBars !=0) && (self.tabBars.count >0)){
int way = 0;
UIImage * image;
if (position > 0) {
way = 8;
//left
image = [UIImage imageNamed:@"left_arrow"];
if( !image){
image = [self makeTriangle:CGRectMake(1, 1,9,48) where:way];
}
imageviewleft = [[UIImageView alloc]initWithImage: image];
imageviewleft.alpha = 0.0;
imageviewleft.frame = CGRectMake(0, sbounds.size.height- 49.0,14,49);
//imageviewleft.bounds = CGRectMake(0, 0,25,49);
[self.superview addSubview:imageviewleft];
[UIView animateWithDuration:0.4 animations:^{
imageviewleft.alpha =0.5;
}];
}
image=nil;
if (position < self.tabBars.count-1){
way = 2;
//right
image = [UIImage imageNamed:@"right_arrow"];
if( !image){
image = [self makeTriangle:CGRectMake(1, 1,9,48) where:way];
}
imageviewright = [[UIImageView alloc]initWithImage: image];
imageviewright.alpha = 0.0;
imageviewright.frame = CGRectMake(sbounds.size.width-14, sbounds.size.height- 49.0,14,49);
// imageviewright.bounds = CGRectMake(290, 0,25,49);
[self.superview addSubview:imageviewright];
[UIView animateWithDuration:0.4 animations:^{
imageviewright.alpha = 0.5;
}];
}
}
}
- (id)initWithFrame:(CGRect)frame{
//self = [super initWithFrame:CGRectMake(0.0, 411.0, 320.0, 49.0)];
// TODO:
UIScreen *screen = [UIScreen mainScreen];
CGRect sbounds = screen.applicationFrame;
self = [super initWithFrame:CGRectMake(0, sbounds.size.height - 49.0, sbounds.size.width, 49.0)];
// Doesn't work. self is nil at this point.
if (self) {
self.indicatorStyle=UIScrollViewIndicatorStyleWhite;
self.pagingEnabled = YES;
self.delegate = self;
self.tabBars = [[[NSMutableArray alloc] init] autorelease];
float x = 0.0;
self.contentSize = CGSizeMake(x, 49.0);
self.arrow = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab-arrow.png"]] autorelease];
}
return self;
}
- (id)initWithFrameAndItems:(CGRect)frame items:(NSArray *)items {
//self = [super initWithFrame:CGRectMake(0.0, 411.0, 320.0, 49.0)];
// TODO:
UIScreen *screen = [UIScreen mainScreen];
CGRect sbounds = screen.applicationFrame;
self = [self initWithFrame:CGRectMake(0, sbounds.size.height - 49.0, sbounds.size.width, 49.0)];
// Doesn't work. self is nil at this point.
if (self) {
[self setItems:items animated:NO];
}
return self;
}
- (void)positionArrowAnimated:(BOOL)animated {
if (self.tabBars.count !=0){
int position = [self currentTabBarTag];
int tagofvisiblebar=-1;
UITabBar *tb = [self.tabBars objectAtIndex:position];
if (tb.selectedItem){
tagofvisiblebar=tb.selectedItem.tag;
}
int tag = [self selectedItemTag];
self.arrow.hidden = !(tagofvisiblebar == tag);
[self.superview bringSubviewToFront:self.arrow];
if (animated) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
}
CGRect f = self.arrow.frame;
UIScreen *screen = [UIScreen mainScreen];
CGRect sbounds = screen.applicationFrame;
int nbitems = tb.items.count;
float pos = (tag % 5) * ((sbounds.size.width/nbitems));
f.origin.x = pos + (((sbounds.size.width/nbitems) / 2) - (f.size.width / 2));
f.origin.y= sbounds.size.height- 55;
self.arrow.frame = f;
if (animated) {
[UIView commitAnimations];
}
}
}
//320
- (void)setBounces:(BOOL)bounces {
if (bounces) {
int count = self.tabBars.count;
if (count > 0) {
if (self.aTabBar == nil)
self.aTabBar = [[[UITabBar alloc] initWithFrame:CGRectMake(-1*self.frame.size.width, 0.0, self.frame.size.width, 49.0)]autorelease];
[self addSubview:self.aTabBar];
if (self.bTabBar == nil)
self.bTabBar = [[[UITabBar alloc] initWithFrame:CGRectMake(count * self.frame.size.width, 0.0, self.frame.size.width, 49.0)] autorelease];
[self addSubview:self.bTabBar];
}
} else {
[self.aTabBar removeFromSuperview];
[self.bTabBar removeFromSuperview];
}
[super setBounces:bounces];
}
- (void)setItems:(NSArray *)items animated:(BOOL)animated {
if (self.tabBars.count !=0){
for (UITabBar *tabBar in self.tabBars) {
int len = 0;
for (int i = [self.tabBars indexOfObject:tabBar] * 5; i < [self.tabBars indexOfObject:tabBar] * 5 + 5; i ++)
if (i < items.count)
len ++;
[tabBar setItems:[items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([self.tabBars indexOfObject:tabBar] * 5, len)]] animated:animated];
}
self.contentSize = CGSizeMake(ceil(items.count / 5.0) * self.frame.size.width, 49.0);
}else{
UIScreen *screen = [UIScreen mainScreen];
CGRect sbounds = screen.applicationFrame;
float x = 0.0;
for (double d = 0; d < ceil(items.count / 5.0); d ++) {
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(x, 0.0, sbounds.size.width, 49.0)];
tabBar.delegate = self;
int len = 0;
for (int i = d * 5; i < d * 5 + 5; i ++)
if (i < items.count)
len ++;
tabBar.items = [items objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(d * 5, len)]];
[self addSubview:tabBar];
[self.tabBars addObject:tabBar];
[tabBar release];
x += self.frame.size.width;
}
self.contentSize = CGSizeMake(x, 49.0);
}
[self positionArrowAnimated:animated];
[self arrowDecoration];
}
- (int)currentTabBarTag {
return self.contentOffset.x / self.frame.size.width;
}
- (int)selectedItemTag {
for (UITabBar *tabBar in self.tabBars)
if (tabBar.selectedItem != nil)
return tabBar.selectedItem.tag;
// No item selected
return 0;
}
- (BOOL)scrollToTabBarWithTag:(int)tag animated:(BOOL)animated {
for (UITabBar *tabBar in self.tabBars)
if ([self.tabBars indexOfObject:tabBar] == tag) {
UITabBar *tabBar = [self.tabBars objectAtIndex:tag];
[self scrollRectToVisible:tabBar.frame animated:animated];
if (animated == NO)
[self scrollViewDidEndDecelerating:self];
return YES;
}
return NO;
}
- (BOOL)selectItemWithTag:(int)tag {
for (UITabBar *tabBar in self.tabBars)
for (UITabBarItem *item in tabBar.items)
if (item.tag == tag) {
tabBar.selectedItem = item;
[self tabBar:tabBar didSelectItem:item];
return YES;
}
return NO;
}
- (void)layoutSubviews {
[super layoutSubviews];
[self positionArrowAnimated:NO];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[infiniTabBarDelegate infiniTabBar:self didScrollToTabBarWithTag:scrollView.contentOffset.x / self.frame.size.width];
[self arrowDecoration];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[self scrollViewDidEndDecelerating:scrollView];
}
- (void)tabBar:(UITabBar *)cTabBar didSelectItem:(UITabBarItem *)item {
if ([infiniTabBarDelegate infiniTabBar:self willSelectItemWithTag:item.tag]){
// Act like a single tab bar
for (UITabBar *tabBar in self.tabBars)
if (tabBar != cTabBar)
tabBar.selectedItem = nil;
[infiniTabBarDelegate infiniTabBar:self didSelectItemWithTag:item.tag];
[self positionArrowAnimated:YES];
}else{
for (UITabBar *tabBar in self.tabBars)
tabBar.selectedItem = nil;
}
}
- (void)dealloc {
[bTabBar release];
[aTabBar release];
[tabBars release];
if (imageviewleft)
[imageviewleft release];
if (imageviewright)
[imageviewright release];
[super dealloc];
}
@end