forked from weissi/FRLayeredNavigationController
/
FRLayeredNavigationController.m
573 lines (472 loc) · 20.9 KB
/
FRLayeredNavigationController.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/* This file is part of FRLayeredNavigationController.
*
* FRLayeredNavigationController 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 3 of the License, or
* (at your option) any later version.
*
* FRLayeredNavigationController 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 FRLayeredNavigationController. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright (c) 2012, Johannes Weiß <weiss@tux4u.de> for factis research GmbH.
*/
#import "FRLayeredNavigationController.h"
#import "FRLayerController.h"
#import "FRLayeredNavigationItem.h"
#import "UIViewController+FRLayeredNavigationController.h"
#import <QuartzCore/QuartzCore.h>
#define FRLayeredNavigationControllerStandardDistance ((float)64)
#define FRLayeredNavigationControllerStandardWidth ((float)400)
#define FRLayeredNavigationControllerSnappingVelocityThreshold ((float)100)
typedef enum {
SnappingPointsMethodNearest,
SnappingPointsMethodCompact,
SnappingPointsMehtodExpand
} SnappingPointsMethod;
@interface FRLayeredNavigationController ()
@property (nonatomic, readwrite, strong) UIPanGestureRecognizer *panGR;
@property (nonatomic, readwrite, strong) NSMutableArray *viewControllers;
@property (nonatomic, readwrite, weak) UIViewController *outOfBoundsViewController;
@property (nonatomic, readwrite, weak) UIView *firstTouchedView;
@end
@implementation FRLayeredNavigationController
#pragma mark - Initialization/dealloc
- (id)initWithRootViewController:(UIViewController *)rootViewController
{
return [self initWithRootViewController:rootViewController configuration:^(FRLayeredNavigationItem *item) {
/* nothing */
}];
}
- (id)initWithRootViewController:(UIViewController *)rootViewController
configuration:(void (^)(FRLayeredNavigationItem *item))configuration
{
self = [super init];
if (self) {
FRLayerController *layeredRC = [[FRLayerController alloc] initWithContentViewController:rootViewController maximumWidth:NO];
_viewControllers = [[NSMutableArray alloc] initWithObjects:layeredRC, nil];
layeredRC.layeredNavigationItem.nextItemDistance = FRLayeredNavigationControllerStandardDistance;
layeredRC.layeredNavigationItem.width = FRLayeredNavigationControllerStandardWidth;
layeredRC.layeredNavigationItem.hasChrome = NO;
configuration(layeredRC.layeredNavigationItem);
_outOfBoundsViewController = nil;
[self addChildViewController:layeredRC];
[layeredRC didMoveToParentViewController:self];
}
return self;
}
- (void)dealloc {
self.panGR.delegate = nil;
}
#pragma mark - UIViewController interface
- (void)loadView
{
self.view = [[UIView alloc] init];
for (FRLayerController *vc in self.viewControllers) {
vc.view.frame = CGRectMake(vc.layeredNavigationItem.currentViewPosition.x,
vc.layeredNavigationItem.currentViewPosition.y,
vc.layeredNavigationItem.width,
self.view.frame.size.height);
vc.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:vc.view];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
self.panGR.maximumNumberOfTouches = 1;
self.panGR.delegate = self;
[self.view addGestureRecognizer:self.panGR];
self.view.backgroundColor = [UIColor clearColor];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)orientation
{
NSLog(@"ORIENTATION");
for (FRLayerController *vc in self.viewControllers) {
CGRect f = vc.view.frame;
f.origin = vc.layeredNavigationItem.currentViewPosition;
if (vc.maximumWidth) {
f.size.width = self.view.bounds.size.width - vc.layeredNavigationItem.initialViewPosition.x;
vc.layeredNavigationItem.width = f.size.width;
}
f.size.height = self.view.bounds.size.height;
vc.view.frame = f;
}
return;
}
- (void)viewWillUnload
{
self.panGR = nil;
self.firstTouchedView = nil;
self.outOfBoundsViewController = nil;
}
- (void)viewDidUnload
{
[super viewDidUnload];
NSLog(@"FRLayeredNavigationController (%@): viewDidUnload", self);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark - UIGestureRecognizer delegate interface
- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
switch (gestureRecognizer.state) {
case UIGestureRecognizerStatePossible: {
//NSLog(@"UIGestureRecognizerStatePossible");
break;
}
case UIGestureRecognizerStateBegan: {
//NSLog(@"UIGestureRecognizerStateBegan");
UIView *touchedView = [gestureRecognizer.view hitTest:[gestureRecognizer locationInView:gestureRecognizer.view]
withEvent:nil];
self.firstTouchedView = touchedView;
break;
}
case UIGestureRecognizerStateChanged: {
//NSLog(@"UIGestureRecognizerStateChanged, vel=%f", [gestureRecognizer velocityInView:firstView].x);
const NSInteger startVcIdx = [self.viewControllers count]-1;
const UIViewController *startVc = [self.viewControllers objectAtIndex:startVcIdx];
[self moveViewControllersXTranslation:[gestureRecognizer translationInView:self.view].x];
/*
[self moveViewControllersStartIndex:startVcIdx
xTranslation:[gestureRecognizer translationInView:self.view].x
withParentIndex:-1
parentLastPosition:CGPointZero
descendentOfTouched:NO];
*/
[gestureRecognizer setTranslation:CGPointZero inView:startVc.view];
break;
}
case UIGestureRecognizerStateEnded: {
//NSLog(@"UIGestureRecognizerStateEnded");
[UIView animateWithDuration:0.2 animations:^{
[self moveToSnappingPointsWithGestureRecognizer:gestureRecognizer];
}];
self.firstTouchedView = nil;
break;
}
default:
break;
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UISlider class]]) {
// prevent recognizing touches on the slider
return NO;
}
return YES;
}
#pragma mark - internal methods
+ (void)viewControllerToInitialPosition:(FRLayerController *)vc
{
const CGPoint initPos = vc.layeredNavigationItem.initialViewPosition;
CGRect f = vc.view.frame;
f.origin = initPos;
vc.layeredNavigationItem.currentViewPosition = initPos;
vc.view.frame = f;
}
+ (BOOL)viewController:(FRLayerController *)vc xTranslation:(CGFloat)origXTranslation bounded:(BOOL)bounded {
BOOL didMoveOutOfBounds = NO;
const FRLayeredNavigationItem *navItem = vc.layeredNavigationItem;
const CGPoint initPos = navItem.initialViewPosition;
if (bounded) {
/* apply translation to fancy item position first and then apply to view */
CGRect f = vc.view.frame;
f.origin = navItem.currentViewPosition;
f.origin.x += origXTranslation;
if (f.origin.x <= initPos.x) {
f.origin.x = initPos.x;
}
vc.view.frame = f;
navItem.currentViewPosition = f.origin;
} else {
CGRect f = vc.view.frame;
CGFloat xTranslation;
if (f.origin.x < initPos.x && origXTranslation < 0) {
/* if view already left from left bound and still moving left, half moving speed */
xTranslation = origXTranslation / 2;
} else {
xTranslation = origXTranslation;
}
f.origin.x += xTranslation;
/* apply translation to frame first */
if (f.origin.x <= initPos.x) {
didMoveOutOfBounds = YES;
navItem.currentViewPosition = initPos;
} else {
navItem.currentViewPosition = f.origin;
}
vc.view.frame = f;
}
return didMoveOutOfBounds;
}
- (void)viewControllersToSnappingPointsMethod:(SnappingPointsMethod)method {
FRLayerController *last = nil;
CGFloat xTranslation = 0;
for (FRLayerController *vc in self.viewControllers) {
const CGPoint myPos = vc.layeredNavigationItem.currentViewPosition;
const CGPoint myInitPos = vc.layeredNavigationItem.initialViewPosition;
const CGFloat curDiff = myPos.x - last.layeredNavigationItem.currentViewPosition.x;
const CGFloat initDiff = myInitPos.x - last.layeredNavigationItem.initialViewPosition.x;
const CGFloat maxDiff = last.view.frame.size.width;
if (xTranslation == 0 && (CGFloatNotEqual(curDiff, initDiff) && CGFloatNotEqual(curDiff, maxDiff))) {
switch (method) {
case SnappingPointsMethodNearest: {
if ((curDiff - initDiff) > (maxDiff - curDiff)) {
/* right snapping point is nearest */
xTranslation = maxDiff - curDiff;
} else {
/* left snapping point is nearest */
xTranslation = initDiff - curDiff;
}
break;
}
case SnappingPointsMethodCompact: {
xTranslation = initDiff - curDiff;
break;
}
case SnappingPointsMehtodExpand: {
xTranslation = maxDiff - curDiff;
break;
}
}
}
[FRLayeredNavigationController viewController:vc xTranslation:xTranslation bounded:YES];
last = vc;
}
}
- (void)moveToSnappingPointsWithGestureRecognizer:(UIPanGestureRecognizer *)g
{
const CGFloat velocity = [g velocityInView:self.view].x;
SnappingPointsMethod method;
if (abs(velocity) > FRLayeredNavigationControllerSnappingVelocityThreshold) {
if (velocity > 0) {
method = SnappingPointsMehtodExpand;
} else {
method = SnappingPointsMethodCompact;
}
} else {
method = SnappingPointsMethodNearest;
}
[self viewControllersToSnappingPointsMethod:method];
}
- (void)moveViewControllersXTranslation:(CGFloat)xTranslationGesture
{
FRLayeredNavigationItem *parentNavItem = nil;
CGPoint parentOldPos = CGPointZero;
BOOL descendentOfTouched = NO;
FRLayerController *rootVC = [self.viewControllers objectAtIndex:0];
for (FRLayerController *me in [self.viewControllers reverseObjectEnumerator]) {
if (rootVC == me) {
break;
}
FRLayeredNavigationItem *meNavItem = me.layeredNavigationItem;
const CGPoint myPos = meNavItem.currentViewPosition;
const CGPoint myInitPos = meNavItem.initialViewPosition;
const CGFloat myWidth = me.view.frame.size.width;
CGPoint myNewPos = myPos;
const CGPoint myOldPos = myPos;
const CGPoint parentPos = parentNavItem.currentViewPosition;
const CGPoint parentInitPos = parentNavItem.initialViewPosition;
CGFloat xTranslation = 0;
if (parentNavItem == nil || !descendentOfTouched) {
xTranslation = xTranslationGesture;
} else {
CGFloat newX = myPos.x;
const CGFloat minDiff = parentInitPos.x - myInitPos.x;
if (parentOldPos.x >= myPos.x + myWidth || parentPos.x >= myPos.x + myWidth) {
/* if snapped to parent's right border, move with parent */
newX = parentPos.x - myWidth;
}
if (parentPos.x - myNewPos.x <= minDiff) {
/* at least minDiff difference between parent and me */
newX = parentPos.x - minDiff;
}
xTranslation = newX - myPos.x;
}
const BOOL isTouchedView = !descendentOfTouched && [self.firstTouchedView isDescendantOfView:me.view];
if (self.outOfBoundsViewController == nil ||
self.outOfBoundsViewController == me ||
xTranslationGesture < 0) {
const BOOL boundedMove = !isTouchedView;
/*
* IF no view controller is out of bounds (too far on the left)
* OR if me who is out of bounds
* OR the translation goes to the left again
* THEN: apply the translation
*/
const BOOL outOfBoundsMove = [FRLayeredNavigationController viewController:me
xTranslation:xTranslation
bounded:boundedMove];
if (outOfBoundsMove) {
/* this move was out of bounds */
self.outOfBoundsViewController = me;
} else if(!outOfBoundsMove && self.outOfBoundsViewController == me) {
/* I have been moved out of bounds some time ago but now I'm back in the bounds :-), so:
* - no one can be out of bounds now
* - I have to be reset to my initial position
* - discard the rest of the translation
*/
self.outOfBoundsViewController = nil;
[FRLayeredNavigationController viewControllerToInitialPosition:me];
break; /* this discards the rest of the translation (i.e. stops the loop) */
}
}
if (isTouchedView) {
NSAssert(!descendentOfTouched, @"cannot be descendent of touched AND touched view");
descendentOfTouched = YES;
}
/* initialize next iteration */
parentNavItem = meNavItem;
parentOldPos = myOldPos;
}
}
- (CGFloat)savePlaceWanted:(CGFloat)pointsWanted;
{
CGFloat xTranslation = 0;
if (pointsWanted <= 0) {
return 0;
}
for (FRLayerController *vc in self.viewControllers) {
const CGFloat initX = vc.layeredNavigationItem.initialViewPosition.x;
const CGFloat currentX = vc.layeredNavigationItem.currentViewPosition.x;
if (initX < currentX + xTranslation) {
xTranslation += initX - (currentX + xTranslation);
}
if (abs(xTranslation) >= pointsWanted) {
break;
}
}
for (FRLayerController *vc in self.viewControllers) {
if (vc == [self.viewControllers lastObject]) {
break;
}
[FRLayeredNavigationController viewController:vc xTranslation:xTranslation bounded:YES];
}
return abs(xTranslation);
}
#pragma mark - Public API
- (void)popViewControllerAnimated:(BOOL)animated
{
UIViewController *vc = [self.viewControllers lastObject];
if ([self.viewControllers count] == 1) {
/* don't remove root view controller */
return;
}
[vc willMoveToParentViewController:nil];
[self.viewControllers removeObject:vc];
CGRect goAwayFrame = CGRectMake(vc.view.frame.origin.x, 1024, vc.view.frame.size.width, vc.view.frame.size.height);
[UIView animateWithDuration:animated ? 0.5 : 0
delay:0
options: UIViewAnimationCurveLinear
animations:^{
vc.view.frame = goAwayFrame;
}
completion:^(BOOL finished) {
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
}];
}
- (void)popToViewController:(UIViewController *)vc animated:(BOOL)animated
{
UIViewController *currentVc;
while ((currentVc = [self.viewControllers lastObject])) {
if (([currentVc class] == [FRLayerController class] &&
((FRLayerController*)currentVc).contentViewController == vc) ||
([currentVc class] != [FRLayerController class] &&
currentVc == vc)) {
break;
}
if ([self.viewControllers count] == 1) {
/* don't remove root view controller */
return;
}
[self popViewControllerAnimated:animated];
}
}
- (void)popToRootViewControllerAnimated:(BOOL)animated
{
[self popToViewController:[self.viewControllers objectAtIndex:0] animated:animated];
}
- (void)pushViewController:(UIViewController *)contentViewController
inFrontOf:(UIViewController *)anchorViewController
maximumWidth:(BOOL)maxWidth
animated:(BOOL)animated
configuration:(void (^)(FRLayeredNavigationItem *item))configuration
{
FRLayerController *newVC = [[FRLayerController alloc]
initWithContentViewController:contentViewController maximumWidth:maxWidth];
const FRLayeredNavigationItem *navItem = newVC.layeredNavigationItem;
const FRLayeredNavigationItem *parentNavItem = anchorViewController.layeredNavigationItem;
[self popToViewController:anchorViewController animated:animated];
CGFloat anchorInitX = anchorViewController.layeredNavigationItem.initialViewPosition.x;
CGFloat anchorCurrentX = anchorViewController.layeredNavigationItem.currentViewPosition.x;
CGFloat anchorWidth = anchorViewController.view.frame.size.width;
CGFloat initX = anchorInitX + (parentNavItem.nextItemDistance > 0 ? parentNavItem.nextItemDistance :
FRLayeredNavigationControllerStandardDistance);
navItem.initialViewPosition = CGPointMake(initX, 0);
navItem.currentViewPosition = CGPointMake(anchorCurrentX + anchorWidth, 0);
navItem.titleView = nil;
navItem.title = nil;
navItem.hasChrome = YES;
configuration(newVC.layeredNavigationItem);
CGFloat width;
if (navItem.width > 0) {
width = navItem.width;
} else {
width = newVC.maximumWidth ? self.view.bounds.size.width - initX : FRLayeredNavigationControllerStandardWidth;
navItem.width = width;
}
CGRect newFrame = CGRectMake(newVC.layeredNavigationItem.currentViewPosition.x,
newVC.layeredNavigationItem.currentViewPosition.y,
width,
self.view.bounds.size.height);
CGRect startFrame = CGRectMake(MAX(1024, newFrame.origin.x),
0,
newFrame.size.width,
newFrame.size.height);
[self.viewControllers addObject:newVC];
[self addChildViewController:newVC];
[self.view addSubview:newVC.view];
[newVC didMoveToParentViewController:self];
newVC.view.frame = startFrame;
[UIView animateWithDuration:animated ? 0.5 : 0
delay:0
options: UIViewAnimationCurveEaseOut
animations:^{
CGFloat saved = [self savePlaceWanted:newFrame.origin.x+width-self.view.bounds.size.width];
newVC.view.frame = CGRectMake(newFrame.origin.x - saved,
newFrame.origin.y,
newFrame.size.width,
newFrame.size.height);
newVC.layeredNavigationItem.currentViewPosition = newVC.view.frame.origin;
}
completion:^(BOOL finished) {
}];
}
- (void)pushViewController:(UIViewController *)contentViewController
inFrontOf:(UIViewController *)anchorViewController
maximumWidth:(BOOL)maxWidth
animated:(BOOL)animated
{
[self pushViewController:contentViewController
inFrontOf:anchorViewController
maximumWidth:maxWidth
animated:animated
configuration:^(FRLayeredNavigationItem *item) {
}];
}
#pragma mark - properties
@synthesize viewControllers = _viewControllers;
@synthesize panGR = _panGR;
@synthesize firstTouchedView = _firstTouchedView;
@synthesize outOfBoundsViewController = _outOfBoundsViewController;
@end