-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathLeveyTabBar.m
146 lines (132 loc) · 4.54 KB
/
LeveyTabBar.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
//
// LeveyTabBar.m
// LeveyTabBarController
//
// Created by Levey Zhu on 12/15/10.
// Copyright 2010 SlyFairy. All rights reserved.
//
#import "LeveyTabBar.h"
@implementation LeveyTabBar
@synthesize backgroundView = _backgroundView;
@synthesize delegate = _delegate;
@synthesize buttons = _buttons;
@synthesize animatedView = _animatedView;
- (id)initWithFrame:(CGRect)frame buttonImages:(NSArray *)imageArray
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
_backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
[self addSubview:_backgroundView];
self.buttons = [NSMutableArray arrayWithCapacity:[imageArray count]];
UIButton *btn;
CGFloat width = 320.0f / [imageArray count];
for (int i = 0; i < [imageArray count]; i++)
{
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.showsTouchWhenHighlighted = YES;
btn.tag = i;
btn.frame = CGRectMake(width * i, 0, width, frame.size.height);
[btn setImage:[[imageArray objectAtIndex:i] objectForKey:@"Default"] forState:UIControlStateNormal];
[btn setImage:[[imageArray objectAtIndex:i] objectForKey:@"Highlighted"] forState:UIControlStateHighlighted];
[btn setImage:[[imageArray objectAtIndex:i] objectForKey:@"Selected"] forState:UIControlStateSelected];
if ([[imageArray objectAtIndex:i] objectForKey:@"Selected|Highlighted"]) {
[btn setImage:[[imageArray objectAtIndex:i] objectForKey:@"Selected|Highlighted"] forState:UIControlStateSelected | UIControlStateHighlighted];
}
[btn addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.buttons addObject:btn];
[self addSubview:btn];
}
}
return self;
}
- (void)setBackgroundImage:(UIImage *)img
{
[_backgroundView setImage:img];
}
- (void)setAnimatedView:(UIImageView *)animatedView
{
[animatedView retain];
[_animatedView release];
_animatedView = animatedView;
[self addSubview:animatedView];
}
- (void)tabBarButtonClicked:(id)sender
{
UIButton *btn = sender;
int index = btn.tag;
if ([_delegate respondsToSelector:@selector(tabBar:shouldSelectIndex:)])
{
if (![_delegate tabBar:self shouldSelectIndex:index])
{
return;
}
}
[self selectTabAtIndex:index];
}
- (void)selectTabAtIndex:(NSInteger)index
{
for (int i = 0; i < [self.buttons count]; i++)
{
UIButton *b = [self.buttons objectAtIndex:i];
b.selected = NO;
}
UIButton *btn = [self.buttons objectAtIndex:index];
btn.selected = YES;
if ([_delegate respondsToSelector:@selector(tabBar:didSelectIndex:)])
{
[_delegate tabBar:self didSelectIndex:btn.tag];
}
[UIView animateWithDuration:0.2f animations:^{
_animatedView.frame = CGRectMake(btn.frame.origin.x, _animatedView.frame.origin.y, _animatedView.frame.size.width, _animatedView.frame.size.height);
}];
NSLog(@"Select index: %d",btn.tag);
}
- (void)removeTabAtIndex:(NSInteger)index
{
// Remove button
[(UIButton *)[self.buttons objectAtIndex:index] removeFromSuperview];
[self.buttons removeObjectAtIndex:index];
// Re-index the buttons
CGFloat width = 320.0f / [self.buttons count];
for (UIButton *btn in self.buttons)
{
if (btn.tag > index)
{
btn.tag --;
}
btn.frame = CGRectMake(width * btn.tag, 0, width, self.frame.size.height);
}
}
- (void)insertTabWithImageDic:(NSDictionary *)dict atIndex:(NSUInteger)index
{
// Re-index the buttons
CGFloat width = 320.0f / ([self.buttons count] + 1);
for (UIButton *b in self.buttons)
{
if (b.tag >= index)
{
b.tag ++;
}
b.frame = CGRectMake(width * b.tag, 0, width, self.frame.size.height);
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.showsTouchWhenHighlighted = YES;
btn.tag = index;
btn.frame = CGRectMake(width * index, 0, width, self.frame.size.height);
[btn setImage:[dict objectForKey:@"Default"] forState:UIControlStateNormal];
[btn setImage:[dict objectForKey:@"Highlighted"] forState:UIControlStateHighlighted];
[btn setImage:[dict objectForKey:@"Selected"] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.buttons insertObject:btn atIndex:index];
[self addSubview:btn];
}
- (void)dealloc
{
[_backgroundView release];
[_buttons release];
[_animatedView release];
[super dealloc];
}
@end