/
CTabViewController.m
179 lines (129 loc) · 4.93 KB
/
CTabViewController.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
//
// CTabViewController.m
// OriginalTabBar
//
// Created by ntaku on 09/05/14.
// Copyright 2009 http://d.hatena.ne.jp/ntaku All rights reserved.
//
#import "CTabViewController.h"
# pragma mark -
# pragma mark CTabViewCell
# pragma mark -
@interface CTabViewCell : UIView
-(void)drawRoundRect:(CGContextRef)context x:(CGFloat)x y:(CGFloat)y width:(CGFloat)w height:(CGFloat)h;
@end
@implementation CTabViewCell
- (void) drawRect: (CGRect) aRect
{
CGContextRef context = UIGraphicsGetCurrentContext();
int margin = 2;
CGFloat width = self.frame.size.width - margin * 2;
CGFloat height = self.frame.size.height - margin * 2;
[self drawRoundRect:context x:margin y:margin+1 width:width height:height];
}
-(void)drawRoundRect:(CGContextRef)context x:(CGFloat)x y:(CGFloat)y width:(CGFloat)w height:(CGFloat)h{
// background color
CGContextSetRGBStrokeColor(context, CTABBAR_CELL_R, CTABBAR_CELL_G, CTABBAR_CELL_B, CTABBAR_CELL_A);
CGContextSetRGBFillColor(context, CTABBAR_CELL_R, CTABBAR_CELL_G, CTABBAR_CELL_B, CTABBAR_CELL_A);
CGContextSetLineWidth(context, 0.2);
CGFloat radius = 4.0;
CGRect rrect = CGRectMake(x, y, w, h);
CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
// draw round rect
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
}
@end
# pragma mark -
# pragma mark CTabViewController
# pragma mark -
@implementation CTabViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.view.backgroundColor = [UIColor whiteColor];
_tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, CTABBAR_TOP, CTABBAR_WIDTH, CTABBAR_HEIGHT)];
_tabBar.delegate = self;
[self.view addSubview:_tabBar];
_tabBarItems = [[NSMutableArray alloc] initWithCapacity:0];
// tab background
UIImageView *bk = [[UIImageView alloc] initWithImage:[UIImage imageNamed:CTABBAR_BGIMG_NAME]];
[_tabBar insertSubview:bk atIndex:0];
[bk release];
// selected item background
_selectedCellBack = [[CTabViewCell alloc] initWithFrame:CGRectZero];
_selectedCellBack.backgroundColor = [UIColor clearColor];
[_tabBar insertSubview:_selectedCellBack atIndex:1];
}
return self;
}
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated{
_viewControllers = [viewControllers copy];
[_tabBarItems removeAllObjects];
UIViewController *ctrl;
for(ctrl in _viewControllers){
UITabBarItem *tmp= [[UITabBarItem alloc] initWithTitle:ctrl.title image:ctrl.tabBarItem.image tag:-1];
tmp.badgeValue = ctrl.tabBarItem.badgeValue;
[_tabBarItems addObject:tmp];
[tmp release];
}
[_tabBar setItems:_tabBarItems];
[self activateViewControllerAt:0];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSUInteger idx = [_tabBarItems indexOfObject:item];
[self activateViewControllerAt:(int)idx];
}
- (void)activateViewControllerAt:(int)index{
selectedIndex = index;
_selectedViewController = (UIViewController *)[_viewControllers objectAtIndex:index];
_tabBar.selectedItem = (UITabBarItem *)[_tabBarItems objectAtIndex:index];
// view controller
UIViewController *vc = (UIViewController *)[_viewControllers objectAtIndex:index];
vc.view.frame = CGRectMake(0, 0, CTABBAR_WIDTH, CTABBAR_TOP);
vc.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:vc.view];
// tab item background
float width = 320 / [_viewControllers count];
float offset = width * index;
CGRect rect = _selectedCellBack.frame;
_selectedCellBack.frame = CGRectMake(offset, rect.origin.y, width, CTABBAR_HEIGHT);
}
- (void)hideTabBar:(BOOL)b animated:(BOOL)b2{
CGRect rect = _tabBar.frame;
rect.origin.y = (b) ? CTABBAR_TOP+CTABBAR_HEIGHT : CTABBAR_TOP;
CGRect rect2 = _selectedViewController.view.frame;
rect2.size.height = (b) ? CTABBAR_TOP+CTABBAR_HEIGHT : CTABBAR_TOP;
if(b2){
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:0.3];
}
_tabBar.frame = rect;
_selectedViewController.view.frame = rect2;
if(b2){
[UIView commitAnimations];
}
}
- (NSUInteger)selectedIndex{
return selectedIndex;
}
- (void)setSelectedIndex:(NSUInteger)index{
[self activateViewControllerAt:(int)index];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[_tabBar release];
[_tabBarItems release];
[_selectedCellBack release];
[_viewControllers release];
[super dealloc];
}
@end