/
IIViewDeckController.txt
2646 lines (2118 loc) · 100 KB
/
IIViewDeckController.txt
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// IIViewDeckController.m
// IIViewDeck
//
// Copyright (C) 2011-2015, ViewDeck
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#define II_FLOAT_EQUAL(x, y) (((x) - (y)) == 0.0f)
#define II_STRING_EQUAL(a, b) ((a == nil && b == nil) || (a != nil && [a isEqualToString:b]))
#define II_CGRectOffsetRightAndShrink(rect, offset) \
({ \
__typeof__(rect) __r = (rect); \
__typeof__(offset) __o = (offset); \
(CGRect) { { __r.origin.x, __r.origin.y }, \
{ __r.size.width - __o, __r.size.height } \
}; \
})
#define II_CGRectOffsetTopAndShrink(rect, offset) \
({ \
__typeof__(rect) __r = (rect); \
__typeof__(offset) __o = (offset); \
(CGRect) { { __r.origin.x, __r.origin.y + __o }, \
{ __r.size.width, __r.size.height - __o } \
}; \
})
#define II_CGRectOffsetBottomAndShrink(rect, offset) \
({ \
__typeof__(rect) __r = (rect); \
__typeof__(offset) __o = (offset); \
(CGRect) { { __r.origin.x, __r.origin.y }, \
{ __r.size.width, __r.size.height - __o} \
}; \
})
#define II_CGRectShrink(rect, w, h) \
({ \
__typeof__(rect) __r = (rect); \
__typeof__(w) __w = (w); \
__typeof__(h) __h = (h); \
(CGRect) { __r.origin, \
{ __r.size.width - __w, __r.size.height - __h} \
}; \
})
#import "IIViewDeckController.h"
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
#import <objc/message.h>
#import "IIWrapController.h"
static const IIViewDeckSide IIViewDeckNoSide = 0;
inline NSString* NSStringFromIIViewDeckSide(IIViewDeckSide side) {
switch (side) {
case IIViewDeckLeftSide:
return @"left";
case IIViewDeckRightSide:
return @"right";
case IIViewDeckNoSide:
return @"no";
default:
return @"unknown";
}
}
static const UIViewAnimationOptions DefaultSwipedAnimationCurve = UIViewAnimationOptionCurveEaseOut;
static NSTimeInterval durationToAnimate(CGFloat pointsToAnimate, CGFloat velocity)
{
NSTimeInterval animationDuration = pointsToAnimate / ABS(velocity);
// adjust duration for easing curve, if necessary
if (DefaultSwipedAnimationCurve != UIViewAnimationOptionCurveLinear) animationDuration *= 1.25;
return animationDuration;
}
#define DEFAULT_DURATION 0.0
@interface IIViewDeckView : UIView {
BOOL _userInteractionEnabled;
BOOL _needsOffsetAdjustment;
}
@property (nonatomic, assign) BOOL allowUserInteractionEnabled;
- (void)setNeedsOffsetAdjustment;
- (BOOL)needsOffsetAdjustment;
@end
@interface UIViewController (UIViewDeckController_ViewContainmentEmulation_Fakes)
- (void)vdc_addChildViewController:(UIViewController *)childController;
- (void)vdc_removeFromParentViewController;
- (void)vdc_willMoveToParentViewController:(UIViewController *)parent;
- (void)vdc_didMoveToParentViewController:(UIViewController *)parent;
@end
@interface IIViewDeckController () <UIGestureRecognizerDelegate> {
CGPoint _panOrigin;
UInt32 _viewAppeared;
BOOL _viewFirstAppeared;
UInt32 _sideAppeared[6];
CGFloat _ledge[5];
CGFloat _offset, _maxLedge;
CGSize _preRotationSize, _preRotationCenterSize;
BOOL _preRotationIsLandscape;
UIInterfaceOrientation _willAppearShouldArrangeViewsAfterRotation;
CGPoint _willAppearOffset;
NSMutableArray* _finishTransitionBlocks;
int _disabledUserInteractions;
BOOL _needsAddPannersIfAllPannersAreInactive;
NSMutableSet* _disabledPanClasses;
}
@property (nonatomic, retain) UIView* referenceView;
@property (nonatomic, readonly) CGRect referenceBounds;
@property (nonatomic, readonly) CGRect centerViewBounds;
@property (nonatomic, readonly) CGRect sideViewBounds;
@property (nonatomic, retain) NSMutableArray* panners;
@property (nonatomic, assign) CGFloat originalShadowRadius;
@property (nonatomic, assign) CGFloat originalShadowOpacity;
@property (nonatomic, retain) UIColor* originalShadowColor;
@property (nonatomic, assign) CGSize originalShadowOffset;
@property (nonatomic, retain) UIBezierPath* originalShadowPath;
@property (nonatomic, retain) UIButton* centerTapper;
@property (nonatomic, weak, readwrite) UIView* centerView;
@property (nonatomic, readonly) UIView* slidingControllerView;
@property (nonatomic, assign) BOOL isObservingView;
@property (nonatomic, assign) BOOL isObservingSelf;
@end
@interface UIViewController (UIViewDeckItem_Internal)
// internal setter for the viewDeckController property on UIViewController
- (void)setViewDeckController:(IIViewDeckController*)viewDeckController;
@end
@interface UIViewController (UIViewDeckController_ViewContainmentEmulation)
- (void)addChildViewController:(UIViewController *)childController;
- (void)removeFromParentViewController;
- (void)willMoveToParentViewController:(UIViewController *)parent;
- (void)didMoveToParentViewController:(UIViewController *)parent;
@end
@implementation IIViewDeckController
#pragma mark - Initalisation and deallocation
static void commonInitWithCenterViewController(IIViewDeckController *self, UIViewController *centerController)
{
self.elastic = YES;
self->_willAppearShouldArrangeViewsAfterRotation = (UIInterfaceOrientation)UIDeviceOrientationUnknown;
self.panningMode = IIViewDeckFullViewPanning;
self.panningCancelsTouchesInView = YES; // let's default to standard IOS behavior.
self.navigationControllerBehavior = IIViewDeckNavigationControllerContained;
self.centerhiddenInteractivity = IIViewDeckCenterHiddenUserInteractive;
self.sizeMode = IIViewDeckLedgeSizeMode;
self->_viewAppeared = 0;
self->_viewFirstAppeared = NO;
self.resizesCenterView = NO;
self.automaticallyUpdateTabBarItems = NO;
self.centerViewOpacity = 1;
self.centerViewCornerRadius = 0;
self.panners = [NSMutableArray array];
self.enabled = YES;
self->_offset = 0;
self.bounceDurationFactor = 0.3;
self.openSlideAnimationDuration = 0.3;
self.closeSlideAnimationDuration = 0.3;
self->_disabledPanClasses = [NSMutableSet setWithObject:[UISlider class]];
#ifndef EXTRA_APPSTORE_SAFETY
[self disablePanOverViewsOfClass:NSClassFromString(@"UITableViewCellReorderControl")];
#endif
self.delegate = nil;
self.delegateMode = IIViewDeckDelegateOnly;
self.originalShadowRadius = 0;
self.originalShadowOffset = CGSizeZero;
self.originalShadowColor = nil;
self.originalShadowOpacity = 0;
self.originalShadowPath = nil;
self.centerController = centerController;
self.leftController = nil;
self.rightController = nil;
self.shadowEnabled = YES;
self->_ledge[IIViewDeckLeftSide] = self->_ledge[IIViewDeckRightSide] = 44;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
commonInitWithCenterViewController(self, nil);
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
commonInitWithCenterViewController(self, nil);
}
return self;
}
- (id)initWithCenterViewController:(UIViewController*)centerController {
if ((self = [super initWithNibName:nil bundle:nil])) {
commonInitWithCenterViewController(self, centerController);
}
return self;
}
- (id)initWithCenterViewController:(UIViewController*)centerController leftViewController:(UIViewController*)leftController {
if ((self = [self initWithCenterViewController:centerController])) {
self.leftController = leftController;
}
return self;
}
- (id)initWithCenterViewController:(UIViewController*)centerController rightViewController:(UIViewController*)rightController {
if ((self = [self initWithCenterViewController:centerController])) {
self.rightController = rightController;
}
return self;
}
- (id)initWithCenterViewController:(UIViewController*)centerController leftViewController:(UIViewController*)leftController rightViewController:(UIViewController*)rightController {
if ((self = [self initWithCenterViewController:centerController])) {
self.leftController = leftController;
self.rightController = rightController;
}
return self;
}
- (id)initWithCenterViewController:(UIViewController*)centerController topViewController:(UIViewController*)topController {
if ((self = [self initWithCenterViewController:centerController])) {
self.topController = topController;
}
return self;
}
- (id)initWithCenterViewController:(UIViewController*)centerController bottomViewController:(UIViewController*)bottomController {
if ((self = [self initWithCenterViewController:centerController])) {
self.bottomController = bottomController;
}
return self;
}
- (id)initWithCenterViewController:(UIViewController*)centerController topViewController:(UIViewController*)topController bottomViewController:(UIViewController*)bottomController {
if ((self = [self initWithCenterViewController:centerController])) {
self.topController = topController;
self.bottomController = bottomController;
}
return self;
}
- (id)initWithCenterViewController:(UIViewController*)centerController leftViewController:(UIViewController*)leftController rightViewController:(UIViewController*)rightController topViewController:(UIViewController*)topController bottomViewController:(UIViewController*)bottomController {
if ((self = [self initWithCenterViewController:centerController])) {
self.leftController = leftController;
self.rightController = rightController;
self.topController = topController;
self.bottomController = bottomController;
}
return self;
}
- (void)cleanup {
self.originalShadowRadius = 0;
self.originalShadowOpacity = 0;
self.originalShadowColor = nil;
self.originalShadowOffset = CGSizeZero;
self.originalShadowPath = nil;
_slidingController = nil;
self.referenceView = nil;
self.centerView = nil;
self.centerTapper = nil;
}
- (void)dealloc {
[self cleanup];
self.centerController.viewDeckController = nil;
self.centerController = nil;
self.leftController.viewDeckController = nil;
self.leftController = nil;
self.rightController.viewDeckController = nil;
self.rightController = nil;
[self removePanners];
self.panners = nil;
// observations related to UIViewController properties
if (self.isObservingSelf) {
@try {
[self removeObserver:self forKeyPath:@"parentViewController"];
[self removeObserver:self forKeyPath:@"presentingViewController"];
self.isObservingSelf = NO;
} @catch(id anException) {
}
}
if (self.isObservingView) {
@try {
[self.view removeObserver:self forKeyPath:@"bounds"];
self.isObservingView = NO;
} @catch(id anException) {
}
}
}
#pragma mark - View Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
UIView *centerView = [[UIView alloc] initWithFrame:self.view.bounds];
centerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
centerView.clipsToBounds = YES;
[self.view addSubview:centerView];
self.centerView = centerView;
[self exchangeViewFromController:nil toController:self.centerController inContainerView:centerView];
self.originalShadowRadius = 0;
self.originalShadowOpacity = 0;
self.originalShadowColor = nil;
self.originalShadowOffset = CGSizeZero;
self.originalShadowPath = nil;
}
- (void)exchangeViewFromController:(UIViewController *)oldController toController:(UIViewController *)newController inContainerView:(UIView *)containerView {
if (oldController == nil && newController == nil) {
return;
}
BOOL viewVisible = (self.view.window != nil);
if (viewVisible) {
[oldController beginAppearanceTransition:NO animated:NO];
[newController beginAppearanceTransition:YES animated:NO];
}
newController.view.frame = containerView.bounds;
newController.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[containerView addSubview:newController.view];
[oldController.view removeFromSuperview];
if (viewVisible) {
[newController endAppearanceTransition];
[oldController endAppearanceTransition];
}
}
#pragma mark - Child Controller Lifecycle
- (void)exchangeViewController:(UIViewController *)oldController withViewController:(UIViewController *)newController viewTransition:(void(^)(void))viewTransition {
[oldController willMoveToParentViewController:nil];
[self addChildViewController:newController];
if (self.isViewLoaded && viewTransition) {
viewTransition();
}
[newController didMoveToParentViewController:self];
[oldController removeFromParentViewController];
}
- (void)setCenterController:(UIViewController *)centerController {
if (_centerController == centerController) {
return;
}
UIViewController *oldController = _centerController;
_centerController = centerController;
[self exchangeViewController:oldController withViewController:centerController viewTransition:^{
[self exchangeViewFromController:oldController toController:centerController inContainerView:self.centerView];
}];
[self setNeedsStatusBarAppearanceUpdate];
// TODO: Start monitoring tab bar items here...
}
- (void)setLeftController:(UIViewController *)leftController {
if (_leftController == leftController) {
return;
}
UIViewController *oldController = _leftController;
_leftController = leftController;
[self exchangeViewController:oldController withViewController:leftController viewTransition:^{
// TODO: Check if left view controller is currently visible and exchange views
}];
}
- (void)setRightController:(UIViewController *)rightController {
if (_rightController == rightController) {
return;
}
UIViewController *oldController = _rightController;
_rightController = rightController;
[self exchangeViewController:oldController withViewController:rightController viewTransition:^{
// TODO: Check if right view controller is currently visible and exchange views
}];
}
#pragma mark left size
- (void)setLeftSize:(CGFloat)leftSize {
[self setLeftSize:leftSize completion:nil];
}
- (void)setLeftSize:(CGFloat)leftSize completion:(void(^)(BOOL finished))completion {
// [self setSize:leftSize forSide:IIViewDeckLeftSide completion:completion];
}
- (CGFloat)leftSize {
return 0.0;//[self sizeForSide:IIViewDeckLeftSide];
}
- (CGFloat)leftViewSize {
return [self ledgeAsSize:_ledge[IIViewDeckLeftSide] mode:IIViewDeckViewSizeMode forSide:IIViewDeckLeftSide];
}
- (CGFloat)leftLedgeSize {
return [self ledgeAsSize:_ledge[IIViewDeckLeftSide] mode:IIViewDeckLedgeSizeMode forSide:IIViewDeckLeftSide];
}
#pragma mark right size
- (void)setRightSize:(CGFloat)rightSize {
[self setRightSize:rightSize completion:nil];
}
- (void)setRightSize:(CGFloat)rightSize completion:(void(^)(BOOL finished))completion {
// [self setSize:rightSize forSide:IIViewDeckRightSide completion:completion];
}
- (CGFloat)rightSize {
return 0.0;//[self sizeForSide:IIViewDeckRightSide];
}
- (CGFloat)rightViewSize {
return [self ledgeAsSize:_ledge[IIViewDeckRightSide] mode:IIViewDeckViewSizeMode forSide:IIViewDeckRightSide];
}
- (CGFloat)rightLedgeSize {
return [self ledgeAsSize:_ledge[IIViewDeckRightSide] mode:IIViewDeckLedgeSizeMode forSide:IIViewDeckRightSide];
}
#pragma mark max size
- (void)setMaxSize:(CGFloat)maxSize {
[self setMaxSize:maxSize completion:nil];
}
- (void)setMaxSize:(CGFloat)maxSize completion:(void(^)(BOOL finished))completion {
if (/*[self sideControllerCount]*/0 > 1) {
NSLog(@"IIViewDeckController: warning: setting maxLedge with more than one side controllers. Value will be ignored.");
return;
}
[self doForControllers:^(UIViewController* controller, IIViewDeckSide side) {
if (controller) {
_maxLedge = [self sizeAsLedge:maxSize forSide:side];
if (_ledge[side] > _maxLedge) {
// [self setSize:maxSize forSide:side completion:completion];
} else if (completion) {
completion(NO);
}
// [self setSlidingFrameForOffset:_offset forOrientation:IIViewDeckOffsetOrientationFromIIViewDeckSide(side) animated:YES]; // should be animated
}
}];
}
- (CGFloat)maxSize {
return _maxLedge;
}
- (CGFloat)sizeAsLedge:(CGFloat)size forSide:(IIViewDeckSide)side {
if (_sizeMode == IIViewDeckLedgeSizeMode)
return size;
else {
return ((side == IIViewDeckLeftSide || side == IIViewDeckRightSide)
? self.referenceBounds.size.width : self.referenceBounds.size.height) - size;
}
}
- (CGFloat)ledgeAsSize:(CGFloat)ledge forSide:(IIViewDeckSide)side {
return [self ledgeAsSize:ledge mode:_sizeMode forSide:side];
}
- (CGFloat)ledgeAsSize:(CGFloat)ledge mode:(IIViewDeckSizeMode)mode forSide:(IIViewDeckSide)side {
if (mode == IIViewDeckLedgeSizeMode)
return ledge;
else
return ((side == IIViewDeckLeftSide || side == IIViewDeckRightSide)
? self.referenceBounds.size.width : self.referenceBounds.size.height) - ledge;
}
#pragma mark - Status Bar
- (UIViewController *)childViewControllerForStatusBarHidden {
return self.centerController;
}
- (UIViewController *)childViewControllerForStatusBarStyle {
return self.centerController;
}
#pragma mark - Rotation
- (BOOL)shouldAutorotate {
if (self.centerController) {
return self.centerController.shouldAutorotate;
}
return [super shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (self.centerController) {
return self.centerController.supportedInterfaceOrientations;
}
return [super supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if (self.centerController) {
return self.centerController.preferredInterfaceOrientationForPresentation;
}
return [super preferredInterfaceOrientationForPresentation];
}
- (void)setLedgeValue:(CGFloat)ledge forSide:(IIViewDeckSide)side {
if (_maxLedge > 0)
ledge = MIN(_maxLedge, ledge);
_ledge[side] = [self performDelegate:@selector(viewDeckController:changesLedge:forSide:) ledge:ledge side:side];
}
#pragma mark - Notify
- (CGFloat)ledgeOffsetForSide:(IIViewDeckSide)viewDeckSide {
switch (viewDeckSide) {
case IIViewDeckLeftSide:
return self.referenceBounds.size.width - _ledge[viewDeckSide];
break;
case IIViewDeckRightSide:
return _ledge[viewDeckSide] - self.referenceBounds.size.width;
break;
}
return 0;
}
- (void)doForControllers:(void(^)(UIViewController* controller, IIViewDeckSide side))action {
if (!action) return;
for (IIViewDeckSide side=IIViewDeckLeftSide; side<=IIViewDeckRightSide; side++) {
action(_controllers[side], side);
}
}
- (UIViewController*)controllerForSide:(IIViewDeckSide)viewDeckSide {
return viewDeckSide == IIViewDeckNoSide ? nil : _controllers[viewDeckSide];
}
- (IIViewDeckSide)oppositeOfSide:(IIViewDeckSide)viewDeckSide {
switch (viewDeckSide) {
case IIViewDeckLeftSide:
return IIViewDeckRightSide;
case IIViewDeckRightSide:
return IIViewDeckLeftSide;
default:
return IIViewDeckNoSide;
}
}
- (IIViewDeckSide)sideForController:(UIViewController*)controller {
for (IIViewDeckSide side=IIViewDeckLeftSide; side<=IIViewDeckRightSide; side++) {
if (_controllers[side] == controller) return side;
}
return NSNotFound;
}
- (BOOL)checkCanOpenSide:(IIViewDeckSide)viewDeckSide {
return ![self isSideOpen:viewDeckSide] && [self checkDelegate:@selector(viewDeckController:shouldOpenViewSide:) side:viewDeckSide];
}
- (BOOL)checkCanCloseSide:(IIViewDeckSide)viewDeckSide {
return ![self isSideClosed:viewDeckSide] && [self checkDelegate:@selector(viewDeckController:shouldCloseViewSide:animated:) side:viewDeckSide];
}
- (void)notifyWillOpenSide:(IIViewDeckSide)viewDeckSide animated:(BOOL)animated {
if (viewDeckSide == IIViewDeckNoSide) return;
[self notifyAppearanceForSide:viewDeckSide animated:animated from:0 to:1];
if ([self isSideClosed:viewDeckSide]) {
[self performDelegate:@selector(viewDeckController:willOpenViewSide:animated:) side:viewDeckSide animated:animated];
}
}
- (void)notifyDidOpenSide:(IIViewDeckSide)viewDeckSide animated:(BOOL)animated {
if (viewDeckSide == IIViewDeckNoSide) return;
[self notifyAppearanceForSide:viewDeckSide animated:animated from:1 to:2];
if ([self isSideOpen:viewDeckSide]) {
[self performDelegate:@selector(viewDeckController:didOpenViewSide:animated:) side:viewDeckSide animated:animated];
}
}
- (void)notifyWillCloseSide:(IIViewDeckSide)viewDeckSide animated:(BOOL)animated {
if (viewDeckSide == IIViewDeckNoSide) return;
[self notifyAppearanceForSide:viewDeckSide animated:animated from:2 to:1];
if (![self isSideClosed:viewDeckSide]) {
[self performDelegate:@selector(viewDeckController:willCloseViewSide:animated:) side:viewDeckSide animated:animated];
}
}
- (void)notifyDidCloseSide:(IIViewDeckSide)viewDeckSide animated:(BOOL)animated {
if (viewDeckSide == IIViewDeckNoSide) return;
[self notifyAppearanceForSide:viewDeckSide animated:animated from:1 to:0];
if ([self isSideClosed:viewDeckSide]) {
[self performDelegate:@selector(viewDeckController:didCloseViewSide:animated:) side:viewDeckSide animated:animated];
[self performDelegate:@selector(viewDeckController:didShowCenterViewFromSide:animated:) side:viewDeckSide animated:animated];
}
}
- (void)notifyDidChangeOffset:(CGFloat)offset panning:(BOOL)panning {
[self performDelegate:@selector(viewDeckController:didChangeOffset:panning:) offset:offset panning:panning];
}
- (void)notifyAppearanceForSide:(IIViewDeckSide)viewDeckSide animated:(BOOL)animated from:(uint)from to:(uint)to {
if (viewDeckSide == IIViewDeckNoSide)
return;
if (_viewAppeared < to) {
_sideAppeared[viewDeckSide] = to;
return;
}
SEL selector = nil;
if (from < to) {
if (_sideAppeared[viewDeckSide] > from)
return;
if (to == 1)
selector = @selector(_forwardViewWillAppear:onViewController:);
else if (to == 2)
selector = @selector(_forwardViewDidAppear:onViewController:);
}
else {
if (_sideAppeared[viewDeckSide] < from)
return;
if (to == 1)
selector = @selector(_forwardViewWillDisappear:onViewController:);
else if (to == 0)
selector = @selector(_forwardViewDidDisappear:onViewController:);
}
_sideAppeared[viewDeckSide] = to;
}
- (void)transitionAppearanceFrom:(uint)from to:(uint)to animated:(BOOL)animated {
SEL selector = nil;
if (from < to) {
if (to == 1)
selector = @selector(_forwardViewWillAppear:onViewController:);
else if (to == 2)
selector = @selector(_forwardViewDidAppear:onViewController:);
}
else {
if (to == 1)
selector = @selector(_forwardViewWillDisappear:onViewController:);
else if (to == 0)
selector = @selector(_forwardViewDidDisappear:onViewController:);
}
[self doForControllers:^(UIViewController *controller, IIViewDeckSide side) {
if (from < to && _sideAppeared[side] <= from)
return;
else if (from > to && _sideAppeared[side] >= from)
return;
}];
}
#pragma mark - controller state
-(void)setCenterhiddenInteractivity:(IIViewDeckCenterHiddenInteractivity)centerhiddenInteractivity {
_centerhiddenInteractivity = centerhiddenInteractivity;
if ([self isAnySideOpen]) {
if (IIViewDeckCenterHiddenIsInteractive(self.centerhiddenInteractivity)) {
[self centerViewVisible];
} else {
[self centerViewHidden];
}
}
}
- (BOOL)isSideClosed:(IIViewDeckSide)viewDeckSide {
if (![self controllerForSide:viewDeckSide])
return YES;
switch (viewDeckSide) {
case IIViewDeckLeftSide:
return CGRectGetMinX(self.slidingControllerView.frame) <= 0;
case IIViewDeckRightSide:
return CGRectGetMaxX(self.slidingControllerView.frame) >= self.referenceBounds.size.width;
default:
return YES;
}
}
- (BOOL)isAnySideOpen {
return [self isSideOpen:IIViewDeckLeftSide] || [self isSideOpen:IIViewDeckRightSide];
}
- (BOOL)isSideOpen:(IIViewDeckSide)viewDeckSide {
if (![self controllerForSide:viewDeckSide])
return NO;
switch (viewDeckSide) {
case IIViewDeckLeftSide:
return II_FLOAT_EQUAL(CGRectGetMinX(self.slidingControllerView.frame), self.referenceBounds.size.width - _ledge[IIViewDeckLeftSide]);
case IIViewDeckRightSide: {
return II_FLOAT_EQUAL(CGRectGetMaxX(self.slidingControllerView.frame), _ledge[IIViewDeckRightSide]);
}
default:
return NO;
}
}
- (BOOL)isSideTransitioning:(IIViewDeckSide)viewDeckSide {
return ![self isSideClosed:viewDeckSide] && ![self isSideOpen:viewDeckSide];
}
- (BOOL)openSideView:(IIViewDeckSide)side animated:(BOOL)animated completion:(IIViewDeckControllerBlock)completed {
return [self openSideView:side animated:animated duration:DEFAULT_DURATION completion:completed];
}
- (BOOL)openSideView:(IIViewDeckSide)side animated:(BOOL)animated duration:(NSTimeInterval)duration completion:(IIViewDeckControllerBlock)completed {
// if there's no controller or we're already open, just run the completion and say we're done.
if (![self controllerForSide:side] || [self isSideOpen:side]) {
if (completed) completed(self, YES);
return YES;
}
// check the delegate to allow opening
if (![self checkCanOpenSide:side]) {
if (completed) completed(self, NO);
return NO;
};
if (![self isSideClosed:[self oppositeOfSide:side]]) {
return [self toggleOpenViewAnimated:animated completion:completed];
}
if (duration == DEFAULT_DURATION) duration = [self openSlideDuration:animated];
__block UIViewAnimationOptions options = UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionBeginFromCurrentState;
IIViewDeckControllerBlock finish = ^(IIViewDeckController *controller, BOOL success) {
if (!success) {
if (completed) completed(self, NO);
return;
}
[self notifyWillOpenSide:side animated:animated];
[self disableUserInteraction];
[UIView animateWithDuration:duration delay:0 options:options animations:^{
[self hide:NO controllerViewForSide:side];
// [self setSlidingFrameForOffset:[self ledgeOffsetForSide:side] forOrientation:IIViewDeckOffsetOrientationFromIIViewDeckSide(side) animated:YES];
[self centerViewHidden];
} completion:^(BOOL finished) {
[self enableUserInteraction];
[self setAccessibilityForCenterTapper]; // update since the frame and the frame's intersection with the window will have changed
if (completed) completed(self, YES);
[self notifyDidOpenSide:side animated:animated];
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);
}];
};
if ([self isSideClosed:side]) {
// try to close any open view first
return [self closeOpenViewAnimated:animated completion:finish];
}
else {
options |= UIViewAnimationOptionCurveEaseOut;
finish(self, YES);
return YES;
}
}
- (void)hide:(BOOL)hidden controllerViewForSide:(IIViewDeckSide)side {
// if ([self sideControllerCount] > 1)
// [self controllerForSide:side].view.hidden = hidden;
// else
// [self controllerForSide:side].view.hidden = NO;
}
- (BOOL)openSideView:(IIViewDeckSide)side bounceOffset:(CGFloat)bounceOffset targetOffset:(CGFloat)targetOffset bounced:(IIViewDeckControllerBounceBlock)bounced completion:(IIViewDeckControllerBlock)completed {
BOOL animated = YES;
// if there's no controller or we're already open, just run the completion and say we're done.
if (![self controllerForSide:side] || [self isSideOpen:side]) {
if (completed) completed(self, YES);
return YES;
}
// check the delegate to allow opening
if (![self checkCanOpenSide:side]) {
if (completed) completed(self, NO);
return NO;
};
UIViewAnimationOptions options = UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionBeginFromCurrentState;
if ([self isSideClosed:side]) options |= UIViewAnimationOptionCurveEaseIn;
return [self closeOpenViewAnimated:animated completion:^(IIViewDeckController *controller, BOOL success) {
if (!success) {
if (completed) completed(self, NO);
return;
}
CGFloat longFactor = _bounceDurationFactor ? _bounceDurationFactor : 1;
CGFloat shortFactor = _bounceOpenSideDurationFactor ? _bounceOpenSideDurationFactor : (_bounceDurationFactor ? 1-_bounceDurationFactor : 1);
// first open the view completely, run the block (to allow changes)
[self notifyWillOpenSide:side animated:animated];
[self disableUserInteraction];
[UIView animateWithDuration:[self openSlideDuration:YES]*longFactor delay:0 options:options animations:^{
[self hide:NO controllerViewForSide:side];
// [self setSlidingFrameForOffset:bounceOffset forOrientation:IIViewDeckOffsetOrientationFromIIViewDeckSide(side) animated:YES];
} completion:^(BOOL finished) {
[self centerViewHidden];
// run block if it's defined
if (bounced) bounced(self);
[self performDelegate:@selector(viewDeckController:didBounceViewSide:openingController:) side:side controller:_controllers[side]];
// now slide the view back to the ledge position
[UIView animateWithDuration:[self openSlideDuration:YES]*shortFactor delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionBeginFromCurrentState animations:^{
// [self setSlidingFrameForOffset:targetOffset forOrientation:IIViewDeckOffsetOrientationFromIIViewDeckSide(side) animated:YES];
} completion:^(BOOL finished2) {
[self enableUserInteraction];
[self setAccessibilityForCenterTapper]; // update since the frame and the frame's intersection with the window will have changed
if (completed) completed(self, YES);
[self notifyDidOpenSide:side animated:animated];
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);
}];
}];
}];
}
- (BOOL)closeSideView:(IIViewDeckSide)side animated:(BOOL)animated completion:(IIViewDeckControllerBlock)completed {
return [self closeSideView:side animated:animated duration:DEFAULT_DURATION completion:completed];
}
- (BOOL)closeSideView:(IIViewDeckSide)side animated:(BOOL)animated duration:(NSTimeInterval)duration completion:(IIViewDeckControllerBlock)completed {
if ([self isSideClosed:side]) {
if (completed) completed(self, YES);
return YES;
}
// check the delegate to allow closing
if (![self checkCanCloseSide:side]) {
if (completed) completed(self, NO);
return NO;
}
if (duration == DEFAULT_DURATION) duration = [self closeSlideDuration:animated];
UIViewAnimationOptions options = UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionBeginFromCurrentState;
options |= [self isSideOpen:side] ? UIViewAnimationOptionCurveEaseInOut : UIViewAnimationOptionCurveEaseOut;
[self notifyWillCloseSide:side animated:animated];
[self disableUserInteraction];
[UIView animateWithDuration:duration delay:0 options:options animations:^{
// [self setSlidingFrameForOffset:0 forOrientation:IIViewDeckOffsetOrientationFromIIViewDeckSide(side) animated:animated];
[self centerViewVisible];
} completion:^(BOOL finished) {
// [self hideAppropriateSideViews];
[self enableUserInteraction];
if (completed) completed(self, YES);
[self notifyDidCloseSide:side animated:animated];
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);
}];
return YES;
}
- (CGFloat)openSlideDuration:(BOOL)animated {
return animated ? self.openSlideAnimationDuration : 0;
}
- (CGFloat)closeSlideDuration:(BOOL)animated {
return animated ? self.closeSlideAnimationDuration : 0;
}
- (BOOL)closeSideView:(IIViewDeckSide)side bounceOffset:(CGFloat)bounceOffset bounced:(IIViewDeckControllerBounceBlock)bounced completion:(IIViewDeckControllerBlock)completed {
if ([self isSideClosed:side]) {
if (completed) completed(self, YES);
return YES;
}
// check the delegate to allow closing
if (![self checkCanCloseSide:side]) {
if (completed) completed(self, NO);
return NO;
}
UIViewAnimationOptions options = UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionBeginFromCurrentState;
if ([self isSideOpen:side]) options |= UIViewAnimationOptionCurveEaseIn;
BOOL animated = YES;
CGFloat longFactor = _bounceDurationFactor ? _bounceDurationFactor : 1;
CGFloat shortFactor = _bounceOpenSideDurationFactor ? _bounceOpenSideDurationFactor : (_bounceDurationFactor ? 1-_bounceDurationFactor : 1);
// first open the view completely, run the block (to allow changes) and close it again.
[self notifyWillCloseSide:side animated:animated];
[self disableUserInteraction];
[UIView animateWithDuration:[self openSlideDuration:YES]*shortFactor delay:0 options:options animations:^{
// [self setSlidingFrameForOffset:bounceOffset forOrientation:IIViewDeckOffsetOrientationFromIIViewDeckSide(side) animated:YES];