/
CPNavigationController.j
274 lines (204 loc) · 7.49 KB
/
CPNavigationController.j
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
/*
* CPNavigationController.j
* CappuccinoExtensions
*
* Created by Nicholas Small.
* Copyright 2009-2010, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import "CPViewController.j"
@import "CPPropertyAnimation.j"
@import "CPNavigationItem.j"
@import "CPNavigationBar.j"
@implementation CPNavigationController : UIViewController
{
CPMutableArray _viewControllers;
CPViewController _visibleViewController;
CPViewController _disappearingViewController;
// Navigation Bar
CPNavigationBar _navigationBar;
Class _navigationBarClass;
BOOL _navigationBarHidden;
id _delegate;
BOOL _isPopping;
BOOL _receivedPopFromNavigationBar;
}
- (id)initWithRootViewController:(CPViewController)aController
{
if (!aController)
return nil;
self = [super init];
if (self)
{
[self _addViewController:aController];
[self setTitle:[aController title]];
_radioItem = [aController radioItem];
_visibleViewController = aController;
}
return self;
}
- (void)pushViewController:(CPViewController)aController animated:(BOOL)animated
{
_isPopping = NO;
_disappearingViewController = [self topViewController];
[self _addViewController:aController];
_visibleViewController = [self topViewController];
[_navigationBar pushNavigationItem:[_visibleViewController navigationItem] animated:animated];
[self _displayViewAnimated:animated];
}
- (void)popToViewController:(CPViewController)aController animated:(BOOL)animated
{
_isPopping = YES;
if ([self topViewController] == [self bottomViewController])
return NO;
_disappearingViewController = [self topViewController];
var index = [_viewControllers indexOfObject:aController],
count = [_viewControllers count] - 1;
for (; count > index; count--)
[_viewControllers removeLastObject];
_visibleViewController = [self topViewController];
if (_receivedPopFromNavigationBar)
_receivedPopFromNavigationBar = NO;
else
[_navigationBar popToNavigationItem:[aController navigationItem] animated:animated];
[self _displayViewAnimated:animated];
}
- (void)popToRootViewControllerAnimated:(BOOL)animated
{
[self popToViewController:[self bottomViewController] animated:animated];
}
- (void)popViewControllerAnimated:(BOOL)animated
{
[self popToViewController:[self previousViewController] animated:animated];
}
- (CPViewController)topViewController
{
return [_viewControllers lastObject];
}
- (CPViewController)bottomViewController
{
return [_viewControllers objectAtIndex:0];
}
- (CPViewController)visibleViewController
{
return _visibleViewController;
}
- (CPViewController)previousViewController
{
var count = [_viewControllers count];
if (count <= 1)
return nil;
return [_viewControllers objectAtIndex:count - 2];
}
// View Manager
- (void)loadView
{
var view = [[_CPLayoutView alloc] init];
view._viewController = self;
_navigationBar = [[(_navigationBarClass || CPNavigationBar) alloc] init];
[_navigationBar setDelegate:self];
[_navigationBar setItems:[self _navigationItems] animated:NO];
[view addSubview:_navigationBar];
[self setView:view];
}
- (void)viewWillAppear:(BOOL)animated
{
[self _displayViewAnimated:NO];
}
// Private API
- (CPArray)_navigationItems
{
var array = [CPMutableArray array];
var count = [_viewControllers count];
for (var i = 0; i < count; i++)
{
var item = [_viewControllers[i] navigationItem];
[array addObject:item];
}
return array;
}
- (BOOL)_addViewController:(CPViewController)aController
{
if ([_viewControllers containsObject:aController])
return NO;
if (!_viewControllers)
_viewControllers = [CPMutableArray array];
aController._parentViewController = self;
[_viewControllers addObject:aController];
return YES;
}
- (void)_displayViewAnimated:(BOOL)animated
{
if (_disappearingViewController)
{
[_disappearingViewController viewWillDisappear:animated];
if (animated)
[self _startTransition];
else
[[_disappearingViewController view] removeFromSuperview];
[_disappearingViewController viewDidDisappear:animated];
}
if (_visibleViewController)
{
var view = [_visibleViewController view];
[_visibleViewController viewWillAppear:animated];
[view setFrameSize:CGSizeMake(CGRectGetWidth([_view bounds]), CGRectGetHeight([_view bounds]) - CGRectGetMaxY([_navigationBar frame]))];
[view setFrameOrigin:CGPointMake(animated ? (_isPopping ? -CGRectGetWidth([view bounds]) : CGRectGetWidth([view bounds])) : 0.0, CGRectGetMaxY([_navigationBar frame]))];
[view setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
[_view addSubview:view];
if (animated)
[self _endTransition];
[_visibleViewController viewDidAppear:animated];
}
}
- (void)_startTransition
{
if (!_disappearingViewController)
return;
var view = [_disappearingViewController view],
anim = [[CPPropertyAnimation alloc] initWithView:view];
if (_isPopping)
[anim addProperty:@"frame" start:CGRectMakeCopy([view frame]) end:CGRectMake([view frame].size.width, [view frame].origin.y, [view frame].size.width, [view frame].size.height)];
else
[anim addProperty:@"frame" start:CGRectMakeCopy([view frame]) end:CGRectMake(-[view frame].size.width, [view frame].origin.y, [view frame].size.width, [view frame].size.height)];
[anim addProperty:@"alphaValue" start:1.0 end:0.3];
[anim setDuration:0.3];
[anim startAnimation];
[anim setDelegate:self];
}
- (void)_endTransition
{
var view = [_visibleViewController view],
anim = [[CPPropertyAnimation alloc] initWithView:view];
[anim addProperty:@"frame" start:CGRectMakeCopy([view frame]) end:CGRectMake(0.0, [view frame].origin.y, [view frame].size.width, [view frame].size.height)];
[anim addProperty:@"alphaValue" start:0.3 end:1.0];
[anim setDuration:0.3];
[anim startAnimation];
}
- (void)animationDidEnd:(CPAnimation)anAnimation
{
var view = [_disappearingViewController view];
[view removeFromSuperview];
[view setAlphaValue:1.0];
}
@end
@implementation CPNavigationController (CPNavigationBar)
- (void)navigationBar:(CPNavigationBar)aBar didPopItem:(CPNavigationItem)anItem
{
_receivedPopFromNavigationBar = YES;
[self popToViewController:anItem._viewController animated:YES];
}
@end