/
CLCascadeNavigationController.m
355 lines (262 loc) · 11.2 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
//
// CLCascadeNavigationController.m
// Cascade
//
// Created by Emil Wojtaszek on 11-05-06.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CLCascadeNavigationController.h"
@interface CLCascadeNavigationController (Private)
- (void) addPagesRoundedCorners;
- (void) addRoundedCorner:(UIRectCorner)rectCorner toPageAtIndex:(NSInteger)index;
@end
@implementation CLCascadeNavigationController
@synthesize viewControllers = _viewControllers;
@synthesize leftInset, widerLeftInset;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
_viewControllers = nil;
_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)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
[_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(pageDidAppear)]) {
[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");
// index of last page
NSUInteger index = [_viewControllers count] - 1;
// pop page from back
NSEnumerator* enumerator = [_viewControllers reverseObjectEnumerator];
// enumarate pages
while ([enumerator nextObject] && _viewControllers.count > 1) {
// pop page at index
[cascadeView popPageAtIndex:index animated:NO];
[_viewControllers removeLastObject];
index--;
}
[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: animated];
// remove all controllers
[_viewControllers removeAllObjects];
// 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) {
// count of views to pop
NSInteger count = [_viewControllers count] - indexOfSender - 1;
// pop views and remove from _viewControllers
for (NSInteger i = count; i>0; i--) {
NSInteger inx = indexOfSender + i;
[_cascadeView popPageAtIndex:inx animated:NO];
[_viewControllers removeLastObject];
}
}
}
// set cascade navigator to view controller
[viewController setCascadeNavigationController: self];
// add controller to array
[self.viewControllers addObject: viewController];
// push view
[_cascadeView pushPage:[viewController view]
fromPage:[sender view]
animated:animated];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (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];
}
}
}
@end