/
CLCascadeNavigationController.m
425 lines (311 loc) · 13.5 KB
/
CLCascadeNavigationController.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// CLCascadeNavigationController.m
// Cascade
//
// Created by Emil Wojtaszek on 11-05-06.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CLCascadeNavigationController.h"
#import "CLViewController.h"
#import "CLSegmentedView.h"
@interface CLCascadeNavigationController (Private)
- (void) addPagesRoundedCorners;
- (void) addRoundedCorner:(UIRectCorner)rectCorner toPageAtIndex:(NSInteger)index;
- (void) popPagesFromLastIndexTo:(NSInteger)index;
- (void) removeAllPageViewControllers;
@end
@implementation CLCascadeNavigationController
@synthesize viewControllers = _viewControllers;
@synthesize leftInset, widerLeftInset;
- (void)dealloc
{
_cascadeView = nil;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// unload all invisible pages in cascadeView
[_cascadeView unloadInvisiblePages];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// set background color
[self.view setBackgroundColor: [UIColor clearColor]];
_viewControllers = [[NSMutableArray alloc] init];
_cascadeView = [[CLCascadeView alloc] initWithFrame:self.view.bounds];
_cascadeView.delegate = self;
_cascadeView.dataSource = self;
[self.view addSubview:_cascadeView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[_cascadeView removeFromSuperview];
_cascadeView = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[self.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[(UIViewController*)obj willRotateToInterfaceOrientation:toInterfaceOrientation
duration:duration];
*stop = NO;
}];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[(UIViewController*)obj didRotateFromInterfaceOrientation:fromInterfaceOrientation];
*stop = NO;
}];
}
- (void)willAnimateRotationToInterfaceOrientation:( UIInterfaceOrientation )interfaceOrientation
duration:( NSTimeInterval )duration
{
[self.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[(UIViewController*)obj willAnimateRotationToInterfaceOrientation:interfaceOrientation
duration:duration];
*stop = NO;
}];
[_cascadeView updateContentLayoutToInterfaceOrientation:interfaceOrientation
duration:duration ];
}
#pragma mark -
#pragma mark Setters & getters
///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat) widerLeftInset {
return _cascadeView.widerLeftInset;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) setWiderLeftInset:(CGFloat)inset {
[_cascadeView setWiderLeftInset: inset];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat) leftInset {
return _cascadeView.leftInset;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) setLeftInset:(CGFloat)inset {
[_cascadeView setLeftInset: inset];
}
#pragma mark -
#pragma marl test
///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIViewController*) rootViewController {
if ([_viewControllers count] > 0) {
return [_viewControllers objectAtIndex: 0];
}
return nil;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIViewController*) lastCascadeViewController {
if ([_viewControllers count] > 0) {
NSUInteger index = [_viewControllers count] - 1;
return [_viewControllers objectAtIndex: index];
}
return nil;
}
#pragma mark -
#pragma marl CLCascadeViewDataSource
///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIView*) cascadeView:(CLCascadeView *)cascadeView pageAtIndex:(NSInteger)index {
return [[_viewControllers objectAtIndex:index] view];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSInteger) numberOfPagesInCascadeView:(CLCascadeView*)cascadeView {
return [_viewControllers count];
}
#pragma mark -
#pragma marl CLCascadeViewDelegate
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) cascadeView:(CLCascadeView*)cascadeView didLoadPage:(UIView*)page {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) cascadeView:(CLCascadeView*)cascadeView didUnloadPage:(UIView*)page {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) cascadeView:(CLCascadeView*)cascadeView didAddPage:(UIView*)page animated:(BOOL)animated {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) cascadeView:(CLCascadeView*)cascadeView didPopPageAtIndex:(NSInteger)index {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) cascadeView:(CLCascadeView*)cascadeView pageDidAppearAtIndex:(NSInteger)index {
if (index > [_viewControllers count] - 1) return;
UIViewController<CLViewControllerDelegate>* controller = [_viewControllers objectAtIndex: index];
if ([controller respondsToSelector:@selector(pageDidAppear)]) {
[controller pageDidAppear];
}
[self addPagesRoundedCorners];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) cascadeView:(CLCascadeView*)cascadeView pageDidDisappearAtIndex:(NSInteger)index {
if (index > [_viewControllers count] - 1) return;
UIViewController<CLViewControllerDelegate>* controller = [_viewControllers objectAtIndex: index];
if ([controller respondsToSelector:@selector(pageDidDisappear)]) {
[controller pageDidDisappear];
}
[self addPagesRoundedCorners];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) cascadeViewDidStartPullingToDetachPages:(CLCascadeView*)cascadeView {
/*
Override this methods to implement own actions, animations
*/
NSLog(@"cascadeViewDidStartPullingToDetachPages");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) cascadeViewDidPullToDetachPages:(CLCascadeView*)cascadeView {
/*
Override this methods to implement own actions, animations
*/
NSLog(@"cascadeViewDidPullToDetachPages");
// pop page from back
[self popPagesFromLastIndexTo:0];
//load first page
[cascadeView loadPageAtIndex:0];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) cascadeViewDidCancelPullToDetachPages:(CLCascadeView*)cascadeView {
/*
Override this methods to implement own actions, animations
*/
NSLog(@"cascadeViewDidCancelPullToDetachPages");
}
#pragma mark -
#pragma mark Calss methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) setRootViewController:(CLViewController*)viewController animated:(BOOL)animated {
// pop all pages
[_cascadeView popAllPagesAnimated: NO];
// remove all controllers
[self removeAllPageViewControllers];
// add root view controller
[self addViewController:viewController sender:nil animated:animated];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) addViewController:(CLViewController*)viewController sender:(CLViewController*)sender animated:(BOOL)animated {
// if in not sent from categoirs view
if (sender) {
// get index of sender
NSInteger indexOfSender = [_viewControllers indexOfObject:sender];
// if sender is not last view controller
if (indexOfSender != [_viewControllers count] - 1) {
// pop views and remove from _viewControllers
[self popPagesFromLastIndexTo:indexOfSender];
}
}
// set cascade navigator to view controller
[viewController setCascadeNavigationController: self];
// add controller to array
[self.viewControllers addObject: viewController];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
[self addChildViewController:viewController];
#endif
// push view
[_cascadeView pushPage:[viewController view]
fromPage:[sender view]
animated:animated];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
[viewController didMoveToParentViewController:self];
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIViewController*) firstVisibleViewController {
NSInteger index = [_cascadeView indexOfFirstVisibleView: YES];
if (index != NSNotFound) {
return [_viewControllers objectAtIndex: index];
}
return nil;
}
#pragma mark -
#pragma mark Private
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) addRoundedCorner:(UIRectCorner)rectCorner toPageAtIndex:(NSInteger)index {
if (index != NSNotFound) {
UIViewController* viewController = [_viewControllers objectAtIndex: index];
if ([viewController isKindOfClass: [CLViewController class]]) {
CLViewController* firstVisibleController = (CLViewController*)viewController;
if ([firstVisibleController showRoundedCorners]) {
CLSegmentedView* view = (CLSegmentedView*)firstVisibleController.view;
[view setShowRoundedCorners: YES];
[view setRectCorner: rectCorner];
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) addPagesRoundedCorners {
// unload all rounded corners
for (id item in [_cascadeView visiblePages]) {
if (item != [NSNull null]) {
if ([item isKindOfClass:[CLSegmentedView class]]) {
CLSegmentedView* view = (CLSegmentedView*)item;
if ([view showRoundedCorners]) {
[view setShowRoundedCorners: NO];
}
}
}
}
// get index of first visible page
NSInteger indexOfFirstVisiblePage = [_cascadeView indexOfFirstVisibleView: NO];
// get index of last visible page
NSInteger indexOfLastVisiblePage = [_cascadeView indexOfLastVisibleView: NO];
if (indexOfLastVisiblePage == indexOfFirstVisiblePage) {
[self addRoundedCorner:UIRectCornerAllCorners toPageAtIndex: indexOfFirstVisiblePage];
} else {
[self addRoundedCorner:UIRectCornerTopLeft | UIRectCornerBottomLeft toPageAtIndex:indexOfFirstVisiblePage];
if (indexOfLastVisiblePage == [_viewControllers count] -1) {
[self addRoundedCorner:UIRectCornerTopRight | UIRectCornerBottomRight toPageAtIndex:indexOfLastVisiblePage];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) popPagesFromLastIndexTo:(NSInteger)toIndex {
if (toIndex < 0) toIndex = 0;
// index of last page
NSUInteger index = [_viewControllers count] - 1;
// pop page from back
NSEnumerator* enumerator = [_viewControllers reverseObjectEnumerator];
// enumarate pages
while ([enumerator nextObject] && _viewControllers.count > toIndex+1) {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
UIViewController* viewController = [_viewControllers objectAtIndex:index];
[viewController willMoveToParentViewController:nil];
#endif
// pop page at index
[_cascadeView popPageAtIndex:index animated:NO];
[_viewControllers removeLastObject];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
[viewController removeFromParentViewController];
#endif
index--;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) removeAllPageViewControllers {
// pop page from back
NSEnumerator* enumerator = [_viewControllers reverseObjectEnumerator];
// enumarate pages
while ([enumerator nextObject]) {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
UIViewController* viewController = [_viewControllers lastObject];
[viewController willMoveToParentViewController:nil];
#endif
[_viewControllers removeLastObject];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
[viewController removeFromParentViewController];
#endif
}
}
@end