-
Notifications
You must be signed in to change notification settings - Fork 10
/
IIViewDeckController.m
2115 lines (1724 loc) · 90.8 KB
/
IIViewDeckController.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
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, Tom Adriaenssen
//
// 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 some LLVM3 macros if the code is compiled with a different compiler (ie LLVMGCC42)
#ifndef __has_feature
#define __has_feature(x) 0
#endif
#ifndef __has_extension
#define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
#endif
#if __has_feature(objc_arc) && __clang_major__ >= 3
#define II_ARC_ENABLED 1
#endif // __has_feature(objc_arc)
#if II_ARC_ENABLED
#define II_RETAIN(xx) ((void)(0))
#define II_RELEASE(xx) ((void)(0))
#define II_AUTORELEASE(xx) (xx)
#else
#define II_RETAIN(xx) [xx retain]
#define II_RELEASE(xx) [xx release]
#define II_AUTORELEASE(xx) [xx autorelease]
#endif
#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 "WrapController.h"
#define DURATION_FAST 0.3
#define DURATION_SLOW 0.3
#define SLIDE_DURATION(animated,duration) ((animated) ? (duration) : 0)
#define OPEN_SLIDE_DURATION(animated) SLIDE_DURATION(animated,DURATION_FAST)
#define CLOSE_SLIDE_DURATION(animated) SLIDE_DURATION(animated,DURATION_SLOW)
@interface IIViewDeckController () <UIGestureRecognizerDelegate>
@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, retain) UIView* centerView;
@property (nonatomic, readonly) UIView* slidingControllerView;
- (void)cleanup;
- (BOOL)closeLeftViewAnimated:(BOOL)animated callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)closeLeftViewAnimated:(BOOL)animated options:(UIViewAnimationOptions)options callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)openLeftViewAnimated:(BOOL)animated callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)openLeftViewAnimated:(BOOL)animated options:(UIViewAnimationOptions)options callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)openLeftViewBouncing:(IIViewDeckControllerBlock)bounced callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)openLeftViewBouncing:(IIViewDeckControllerBlock)bounced options:(UIViewAnimationOptions)options callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)closeRightViewAnimated:(BOOL)animated callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)closeRightViewAnimated:(BOOL)animated options:(UIViewAnimationOptions)options callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)openRightViewAnimated:(BOOL)animated callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)openRightViewAnimated:(BOOL)animated options:(UIViewAnimationOptions)options callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)openRightViewBouncing:(IIViewDeckControllerBlock)bounced callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)openRightViewBouncing:(IIViewDeckControllerBlock)bounced options:(UIViewAnimationOptions)options callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (CGRect)slidingRectForOffset:(CGFloat)offset;
- (CGSize)slidingSizeForOffset:(CGFloat)offset;
- (NSArray *)bouncingValuesForPosition:(CGFloat)position adjustingLeft:(BOOL)left maximumBounce:(CGFloat)maxBounce numberOfBounces:(CGFloat)numberOfBounces dampingFactor:(CGFloat)zeta duration:(NSTimeInterval)duration;
- (void)setSlidingFrameForOffset:(CGFloat)frame;
- (void)hideAppropriateSideViews;
- (void)reapplySideController:(__strong UIViewController **)controllerStore;
- (BOOL)setSlidingAndReferenceViews;
- (void)applyShadowToSlidingView;
- (void)restoreShadowToSlidingView;
- (void)arrangeViewsAfterRotation;
- (CGFloat)relativeStatusBarHeight;
- (void)centerViewVisible;
- (void)centerViewHidden;
- (void)centerTapped;
- (void)addPanners;
- (void)removePanners;
- (BOOL)checkDelegate:(SEL)selector animated:(BOOL)animated;
- (void)performDelegate:(SEL)selector animated:(BOOL)animated;
- (void)performOffsetDelegate:(SEL)selector offset:(CGFloat)offset;
- (void)relayAppearanceMethod:(void(^)(UIViewController* controller))relay;
- (void)relayAppearanceMethod:(void(^)(UIViewController* controller))relay forced:(BOOL)forced;
@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;
- (BOOL)vdc_shouldRelay;
- (void)vdc_viewWillAppear:(bool)animated;
- (void)vdc_viewDidAppear:(bool)animated;
- (void)vdc_viewWillDisappear:(bool)animated;
- (void)vdc_viewDidDisappear:(bool)animated;
@end
@implementation IIViewDeckController
@synthesize panningMode = _panningMode;
@synthesize panners = _panners;
@synthesize referenceView = _referenceView;
@synthesize slidingController = _slidingController;
@synthesize centerController = _centerController;
@synthesize leftController = _leftController;
@synthesize rightController = _rightController;
@synthesize leftLedge = _leftLedge;
@synthesize rightLedge = _rightLedge;
@synthesize maxLedge = _maxLedge;
@synthesize resizesCenterView = _resizesCenterView;
@synthesize originalShadowOpacity = _originalShadowOpacity;
@synthesize originalShadowPath = _originalShadowPath;
@synthesize originalShadowRadius = _originalShadowRadius;
@synthesize originalShadowColor = _originalShadowColor;
@synthesize originalShadowOffset = _originalShadowOffset;
@synthesize delegate = _delegate;
@synthesize navigationControllerBehavior = _navigationControllerBehavior;
@synthesize panningView = _panningView;
@synthesize centerhiddenInteractivity = _centerhiddenInteractivity;
@synthesize centerTapper = _centerTapper;
@synthesize centerView = _centerView;
@synthesize rotationBehavior = _rotationBehavior;
@synthesize enabled = _enabled;
@synthesize elastic = _elastic;
@synthesize automaticallyUpdateTabBarItems = _automaticallyUpdateTabBarItems;
@synthesize panningGestureDelegate = _panningGestureDelegate;
#pragma mark - Initalisation and deallocation
- (id)initWithCoder:(NSCoder *)aDecoder
{
return [self initWithCenterViewController:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return [self initWithCenterViewController:nil];
}
- (id)initWithCenterViewController:(UIViewController*)centerController {
if ((self = [super initWithNibName:nil bundle:nil])) {
_elastic = YES;
_panningMode = IIViewDeckFullViewPanning;
_navigationControllerBehavior = IIViewDeckNavigationControllerContained;
_centerhiddenInteractivity = IIViewDeckCenterHiddenUserInteractive;
_rotationBehavior = IIViewDeckRotationKeepsLedgeSizes;
_viewAppeared = NO;
_resizesCenterView = NO;
_automaticallyUpdateTabBarItems = NO;
self.panners = [NSMutableArray array];
self.enabled = YES;
self.originalShadowRadius = 0;
self.originalShadowOffset = CGSizeZero;
self.originalShadowColor = nil;
self.originalShadowOpacity = 0;
self.originalShadowPath = nil;
_slidingController = nil;
self.centerController = centerController;
self.leftController = nil;
self.rightController = nil;
self.leftLedge = 44;
self.rightLedge = 44;
}
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;
}
- (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.panners = nil;
#if !II_ARC_ENABLED
[super dealloc];
#endif
}
#pragma mark - Memory management
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[self.centerController didReceiveMemoryWarning];
[self.leftController didReceiveMemoryWarning];
[self.rightController didReceiveMemoryWarning];
}
#pragma mark - Bookkeeping
- (NSArray*)controllers {
NSMutableArray *result = [NSMutableArray array];
if (self.centerController) [result addObject:self.centerController];
if (self.leftController) [result addObject:self.leftController];
if (self.rightController) [result addObject:self.rightController];
return [NSArray arrayWithArray:result];
}
- (CGRect)referenceBounds {
return self.referenceView.bounds;
}
- (CGFloat)relativeStatusBarHeight {
if (![self.referenceView isKindOfClass:[UIWindow class]])
return 0;
return [self statusBarHeight];
}
- (CGFloat)statusBarHeight {
return UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)
? [UIApplication sharedApplication].statusBarFrame.size.width
: [UIApplication sharedApplication].statusBarFrame.size.height;
}
- (CGRect)centerViewBounds {
if (self.navigationControllerBehavior == IIViewDeckNavigationControllerContained)
return self.referenceBounds;
return II_CGRectShrink(self.referenceBounds, 0, [self relativeStatusBarHeight] + (self.navigationController.navigationBarHidden ? 0 : self.navigationController.navigationBar.frame.size.height));
}
- (CGRect)sideViewBounds {
if (self.navigationControllerBehavior == IIViewDeckNavigationControllerContained)
return self.referenceBounds;
return II_CGRectOffsetTopAndShrink(self.referenceBounds, [self relativeStatusBarHeight]);
}
- (CGFloat)limitOffset:(CGFloat)offset {
if (_leftController && _rightController) return offset;
if (_leftController && self.maxLedge > 0) {
CGFloat left = self.referenceBounds.size.width - self.maxLedge;
offset = MAX(offset, left);
}
else if (_rightController && self.maxLedge > 0) {
CGFloat right = self.maxLedge - self.referenceBounds.size.width;
offset = MIN(offset, right);
}
return offset;
}
- (CGRect)slidingRectForOffset:(CGFloat)offset {
offset = [self limitOffset:offset];
return (CGRect) { {self.resizesCenterView && offset < 0 ? 0 : offset, 0}, [self slidingSizeForOffset:offset] };
}
- (CGSize)slidingSizeForOffset:(CGFloat)offset {
if (!self.resizesCenterView) return self.referenceBounds.size;
offset = [self limitOffset:offset];
if (offset < 0)
return (CGSize) { self.centerViewBounds.size.width + offset, self.centerViewBounds.size.height };
return (CGSize) { self.centerViewBounds.size.width - offset, self.centerViewBounds.size.height };
}
-(void)setSlidingFrameForOffset:(CGFloat)offset {
_offset = [self limitOffset:offset];
self.slidingControllerView.frame = [self slidingRectForOffset:_offset];
[self performOffsetDelegate:@selector(viewDeckController:slideOffsetChanged:) offset:_offset];
}
- (void)hideAppropriateSideViews {
self.leftController.view.hidden = CGRectGetMinX(self.slidingControllerView.frame) <= 0;
self.rightController.view.hidden = CGRectGetMaxX(self.slidingControllerView.frame) >= self.referenceBounds.size.width;
}
- (NSArray *)bouncingValuesForPosition:(CGFloat)position adjustingLeft:(BOOL)left maximumBounce:(CGFloat)maxBounce numberOfBounces:(CGFloat)numberOfBounces dampingFactor:(CGFloat)zeta duration:(NSTimeInterval)duration {
// Underdamped, Free Vibration of a SDOF System
// u(t) = abs(e^(-zeta * wn * t) * ((Vo/wd) * sin(wd * t))
// Vo, initial velocity, is calculated to provide the desired maxBounce and
// animation duration. The damped period (wd) and distance of the maximum (first)
// bounce can be controlled either via the initial condition Vo or the damping
// factor zeta for a desired duration, Vo is simpler mathematically.
NSUInteger steps = (NSUInteger)MIN(floorf(duration * 100.0f), 100);
float time = 0.0;
NSMutableArray *values = [NSMutableArray arrayWithCapacity:steps];
double offset = 0.0;
float Td = (2.0f * duration) / numberOfBounces; //Damped period, calculated to give the number of bounces desired in the duration specified (2 bounces per Td)
float wd = (2.0f * M_PI)/Td; // Damped frequency
zeta = MIN(MAX(0.0001f, zeta), 0.9999f); // For an underdamped system, we must have 0 < zeta < 1
float zetaFactor = sqrtf(1 - powf(zeta, 2.0f)); // Used in multiple places
float wn = wd/zetaFactor; // Natural frequency
float Vo = maxBounce * wd/(expf(-zeta/zetaFactor * (0.18f * Td) * wd) * sinf(0.18f * Td * wd));
for (int t = 0; t < steps; t++) {
time = (t / (float)steps) * duration;
offset = abs(expf(-zeta * wn * time) * ((Vo / wd) * sin(wd * time)));
offset = (left ? 1 : -1) * [self limitOffset:offset] + position;
[values addObject:[NSNumber numberWithFloat:offset]];
}
return values;
}
#pragma mark - ledges
- (void)setLeftLedge:(CGFloat)leftLedge {
// Compute the final ledge in two steps. This prevents a strange bug where
// nesting MAX(X, MIN(Y, Z)) with miniscule referenceBounds returns a bogus near-zero value.
CGFloat minLedge = MIN(self.referenceBounds.size.width, leftLedge);
leftLedge = MAX(leftLedge, minLedge);
if (_viewAppeared && II_FLOAT_EQUAL(self.slidingControllerView.frame.origin.x, self.referenceBounds.size.width - _leftLedge)) {
if (leftLedge < _leftLedge) {
[UIView animateWithDuration:CLOSE_SLIDE_DURATION(YES) animations:^{
[self setSlidingFrameForOffset:self.referenceBounds.size.width - leftLedge];
}];
}
else if (leftLedge > _leftLedge) {
[UIView animateWithDuration:OPEN_SLIDE_DURATION(YES) animations:^{
[self setSlidingFrameForOffset:self.referenceBounds.size.width - leftLedge];
}];
}
}
_leftLedge = leftLedge;
}
- (void)setLeftLedge:(CGFloat)leftLedge completion:(void(^)(BOOL finished))completion {
// Compute the final ledge in two steps. This prevents a strange bug where
// nesting MAX(X, MIN(Y, Z)) with miniscule referenceBounds returns a bogus near-zero value.
CGFloat minLedge = MIN(self.referenceBounds.size.width, leftLedge);
leftLedge = MAX(leftLedge, minLedge);
if (_viewAppeared && II_FLOAT_EQUAL(self.slidingControllerView.frame.origin.x, self.referenceBounds.size.width - _leftLedge)) {
if (leftLedge < _leftLedge) {
[UIView animateWithDuration:CLOSE_SLIDE_DURATION(YES) animations:^{
[self setSlidingFrameForOffset:self.referenceBounds.size.width - leftLedge];
} completion:completion];
}
else if (leftLedge > _leftLedge) {
[UIView animateWithDuration:OPEN_SLIDE_DURATION(YES) animations:^{
[self setSlidingFrameForOffset:self.referenceBounds.size.width - leftLedge];
} completion:completion];
}
}
_leftLedge = leftLedge;
}
- (void)setRightLedge:(CGFloat)rightLedge {
// Compute the final ledge in two steps. This prevents a strange bug where
// nesting MAX(X, MIN(Y, Z)) with miniscule referenceBounds returns a bogus near-zero value.
CGFloat minLedge = MIN(self.referenceBounds.size.width, rightLedge);
rightLedge = MAX(rightLedge, minLedge);
if (_viewAppeared && II_FLOAT_EQUAL(self.slidingControllerView.frame.origin.x, _rightLedge - self.referenceBounds.size.width)) {
if (rightLedge < _rightLedge) {
[UIView animateWithDuration:CLOSE_SLIDE_DURATION(YES) animations:^{
[self setSlidingFrameForOffset:rightLedge - self.referenceBounds.size.width];
}];
}
else if (rightLedge > _rightLedge) {
[UIView animateWithDuration:OPEN_SLIDE_DURATION(YES) animations:^{
[self setSlidingFrameForOffset:rightLedge - self.referenceBounds.size.width];
}];
}
}
_rightLedge = rightLedge;
}
- (void)setRightLedge:(CGFloat)rightLedge completion:(void(^)(BOOL finished))completion {
// Compute the final ledge in two steps. This prevents a strange bug where
// nesting MAX(X, MIN(Y, Z)) with miniscule referenceBounds returns a bogus near-zero value.
CGFloat minLedge = MIN(self.referenceBounds.size.width, rightLedge);
rightLedge = MAX(rightLedge, minLedge);
if (_viewAppeared && II_FLOAT_EQUAL(self.slidingControllerView.frame.origin.x, _rightLedge - self.referenceBounds.size.width)) {
if (rightLedge < _rightLedge) {
[UIView animateWithDuration:CLOSE_SLIDE_DURATION(YES) animations:^{
[self setSlidingFrameForOffset:rightLedge - self.referenceBounds.size.width];
} completion:completion];
}
else if (rightLedge > _rightLedge) {
[UIView animateWithDuration:OPEN_SLIDE_DURATION(YES) animations:^{
[self setSlidingFrameForOffset:rightLedge - self.referenceBounds.size.width];
} completion:completion];
}
}
_rightLedge = rightLedge;
}
- (void)setMaxLedge:(CGFloat)maxLedge {
_maxLedge = maxLedge;
if (_leftController && _rightController) {
NSLog(@"IIViewDeckController: warning: setting maxLedge with 2 side controllers. Value will be ignored.");
return;
}
if (_leftController && _leftLedge > _maxLedge) {
self.leftLedge = _maxLedge;
}
else if (_rightController && _rightLedge > _maxLedge) {
self.rightLedge = _maxLedge;
}
[self setSlidingFrameForOffset:_offset];
}
#pragma mark - View lifecycle
- (void)loadView
{
_offset = 0;
_viewAppeared = NO;
self.view = II_AUTORELEASE([[UIView alloc] init]);
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.clipsToBounds = YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.centerView = II_AUTORELEASE([[UIView alloc] init]);
self.centerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.centerView.autoresizesSubviews = YES;
self.centerView.clipsToBounds = YES;
[self.view addSubview:self.centerView];
self.originalShadowRadius = 0;
self.originalShadowOpacity = 0;
self.originalShadowColor = nil;
self.originalShadowOffset = CGSizeZero;
self.originalShadowPath = nil;
}
- (void)viewDidUnload
{
[self cleanup];
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
BOOL wasntAppeared = !_viewAppeared;
[self.view addObserver:self forKeyPath:@"bounds" options:NSKeyValueChangeSetting context:nil];
void(^applyViews)(void) = ^{
[self.centerController.view removeFromSuperview];
[self.centerView addSubview:self.centerController.view];
[self.leftController.view removeFromSuperview];
[self.referenceView insertSubview:self.leftController.view belowSubview:self.slidingControllerView];
[self.rightController.view removeFromSuperview];
[self.referenceView insertSubview:self.rightController.view belowSubview:self.slidingControllerView];
[self reapplySideController:&_leftController];
[self reapplySideController:&_rightController];
[self setSlidingFrameForOffset:_offset];
self.slidingControllerView.hidden = NO;
self.centerView.frame = self.centerViewBounds;
self.centerController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.centerController.view.frame = self.centerView.bounds;
self.leftController.view.frame = self.sideViewBounds;
self.leftController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.rightController.view.frame = self.sideViewBounds;
self.rightController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self applyShadowToSlidingView];
};
if ([self setSlidingAndReferenceViews])
applyViews();
_viewAppeared = YES;
// after 0.01 sec, since in certain cases the sliding view is reset.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
if (!self.referenceView) {
[self setSlidingAndReferenceViews];
applyViews();
}
[self setSlidingFrameForOffset:_offset];
[self hideAppropriateSideViews];
});
[self addPanners];
if (self.slidingControllerView.frame.origin.x == 0.0f)
[self centerViewVisible];
else
[self centerViewHidden];
[self relayAppearanceMethod:^(UIViewController *controller) {
[controller viewWillAppear:animated];
} forced:wasntAppeared];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self relayAppearanceMethod:^(UIViewController *controller) {
[controller viewDidAppear:animated];
}];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self relayAppearanceMethod:^(UIViewController *controller) {
[controller viewWillDisappear:animated];
}];
[self removePanners];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
@try {
[self.view removeObserver:self forKeyPath:@"bounds"];
} @catch(id anException){
//do nothing, obviously it wasn't attached because an exception was thrown
}
[self relayAppearanceMethod:^(UIViewController *controller) {
[controller viewDidDisappear:animated];
}];
}
#pragma mark - rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
_preRotationWidth = self.referenceBounds.size.width;
_preRotationCenterWidth = self.centerView.bounds.size.width;
if (self.rotationBehavior == IIViewDeckRotationKeepsViewSizes) {
_leftWidth = self.leftController.view.frame.size.width;
_rightWidth = self.rightController.view.frame.size.width;
}
BOOL should = YES;
if (self.centerController)
should = [self.centerController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
return should;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self relayAppearanceMethod:^(UIViewController *controller) {
[controller willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}];
[self arrangeViewsAfterRotation];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self restoreShadowToSlidingView];
[self relayAppearanceMethod:^(UIViewController *controller) {
[controller willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[self applyShadowToSlidingView];
[self relayAppearanceMethod:^(UIViewController *controller) {
[controller didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}];
}
- (void)arrangeViewsAfterRotation {
if (_preRotationWidth <= 0) return;
CGFloat offset = self.slidingControllerView.frame.origin.x;
if (self.resizesCenterView && offset == 0) {
offset = offset + (_preRotationCenterWidth - _preRotationWidth);
}
if (self.rotationBehavior == IIViewDeckRotationKeepsLedgeSizes) {
if (offset > 0) {
offset = self.referenceBounds.size.width - _preRotationWidth + offset;
}
else if (offset < 0) {
offset = offset + _preRotationWidth - self.referenceBounds.size.width;
}
}
else {
self.leftLedge = self.leftLedge + self.referenceBounds.size.width - _preRotationWidth;
self.rightLedge = self.rightLedge + self.referenceBounds.size.width - _preRotationWidth;
self.maxLedge = self.maxLedge + self.referenceBounds.size.width - _preRotationWidth;
}
[self setSlidingFrameForOffset:offset];
_preRotationWidth = 0;
}
#pragma mark - controller state
- (BOOL)leftControllerIsClosed {
return !self.leftController || CGRectGetMinX(self.slidingControllerView.frame) <= 0;
}
- (BOOL)rightControllerIsClosed {
return !self.rightController || CGRectGetMaxX(self.slidingControllerView.frame) >= self.referenceBounds.size.width;
}
- (BOOL)leftControllerIsOpen {
return self.leftController && CGRectGetMinX(self.slidingControllerView.frame) < self.referenceBounds.size.width && CGRectGetMinX(self.slidingControllerView.frame) >= self.rightLedge;
}
- (BOOL)rightControllerIsOpen {
return self.rightController && CGRectGetMaxX(self.slidingControllerView.frame) < self.referenceBounds.size.width && CGRectGetMaxX(self.slidingControllerView.frame) >= self.leftLedge;
}
- (void)showCenterView {
[self showCenterView:YES];
}
- (void)showCenterView:(BOOL)animated {
[self showCenterView:animated completion:nil];
}
- (void)showCenterView:(BOOL)animated completion:(IIViewDeckControllerBlock)completed {
BOOL mustRunCompletion = completed != nil;
if (self.leftController && !self.leftController.view.hidden) {
[self closeLeftViewAnimated:animated completion:completed];
mustRunCompletion = NO;
}
if (self.rightController && !self.rightController.view.hidden) {
[self closeRightViewAnimated:animated completion:completed];
mustRunCompletion = NO;
}
if (mustRunCompletion)
completed(self);
}
- (BOOL)bounceLeftView {
return [self bounceLeftViewWithCompletion:nil];
}
- (BOOL)bounceLeftViewWithCompletion:(IIViewDeckControllerBlock)completed {
return [self bounceLeftViewToDistance:40.0f duration:1.2f callDelegate:YES completion:completed];
}
- (BOOL)bounceLeftViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
return [self bounceLeftViewToDistance:distance duration:duration numberOfBounces:4.0f dampingFactor:0.40f callDelegate:YES completion:completed];
}
- (BOOL)bounceLeftViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration numberOfBounces:(CGFloat)numberOfBounces dampingFactor:(CGFloat)zeta callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
if (!self.leftController || II_FLOAT_EQUAL(CGRectGetMinX(self.slidingControllerView.frame), self.leftLedge)) return YES;
// check the delegate to allow bouncing
if (callDelegate && ![self checkDelegate:@selector(viewDeckControllerWillBounceLeftView:animated:) animated:YES]) return NO;
// also close the right view if it's open. Since the delegate can cancel the close, check the result.
if (callDelegate && ![self closeRightViewAnimated:YES options:0 callDelegate:YES completion:nil]) return NO;
// check for in-flight animation, do not add another if so
if ([self.slidingControllerView.layer animationForKey:@"bounceAnimation"]) {
return NO;
}
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.duration = duration;
animation.values = [self bouncingValuesForPosition:self.slidingControllerView.layer.position.x adjustingLeft:YES maximumBounce:distance numberOfBounces:numberOfBounces dampingFactor:zeta duration:duration];
animation.removedOnCompletion = YES;
self.leftController.view.hidden = NO;
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:duration] forKey:kCATransactionAnimationDuration];
[CATransaction setCompletionBlock:^{
// do not re-hide if center view has been panned mid-animation
if (_offset == 0.0f) {
self.rightController.view.hidden = YES;
}
// perform completion and delegate call
if (completed) completed(self);
if (callDelegate) [self performDelegate:@selector(viewDeckControllerDidBounceLeftView:animated:) animated:YES];
}];
[self.slidingControllerView.layer addAnimation:animation forKey:@"bounceAnimation"];
[CATransaction commit];
return YES;
}
- (BOOL)toggleLeftView {
return [self toggleLeftViewAnimated:YES];
}
- (BOOL)openLeftView {
return [self openLeftViewAnimated:YES];
}
- (BOOL)closeLeftView {
return [self closeLeftViewAnimated:YES];
}
- (BOOL)toggleLeftViewAnimated:(BOOL)animated {
return [self toggleLeftViewAnimated:animated completion:nil];
}
- (BOOL)toggleLeftViewAnimated:(BOOL)animated completion:(IIViewDeckControllerBlock)completed {
if ([self leftControllerIsClosed])
return [self openLeftViewAnimated:animated completion:completed];
else
return [self closeLeftViewAnimated:animated completion:completed];
}
- (BOOL)openLeftViewAnimated:(BOOL)animated {
return [self openLeftViewAnimated:animated completion:nil];
}
- (BOOL)openLeftViewAnimated:(BOOL)animated completion:(IIViewDeckControllerBlock)completed {
return [self openLeftViewAnimated:animated options:UIViewAnimationOptionCurveEaseInOut callDelegate:YES completion:completed];
}
- (BOOL)openLeftViewAnimated:(BOOL)animated callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
return [self openLeftViewAnimated:animated options:UIViewAnimationOptionCurveEaseInOut callDelegate:callDelegate completion:completed];
}
- (BOOL)openLeftViewAnimated:(BOOL)animated options:(UIViewAnimationOptions)options callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
if (!self.leftController || II_FLOAT_EQUAL(CGRectGetMinX(self.slidingControllerView.frame), self.leftLedge)) return YES;
// check the delegate to allow opening
if (callDelegate && ![self checkDelegate:@selector(viewDeckControllerWillOpenLeftView:animated:) animated:animated]) return NO;
// also close the right view if it's open. Since the delegate can cancel the close, check the result.
if (callDelegate && ![self closeRightViewAnimated:animated options:options callDelegate:callDelegate completion:completed]) return NO;
[UIView animateWithDuration:OPEN_SLIDE_DURATION(animated) delay:0 options:options | UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionBeginFromCurrentState animations:^{
self.leftController.view.hidden = NO;
[self setSlidingFrameForOffset:self.referenceBounds.size.width - self.leftLedge];
[self centerViewHidden];
} completion:^(BOOL finished) {
if (completed) completed(self);
if (callDelegate) [self performDelegate:@selector(viewDeckControllerDidOpenLeftView:animated:) animated:animated];
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);
}];
return YES;
}
- (BOOL)openLeftViewBouncing:(IIViewDeckControllerBlock)bounced {
return [self openLeftViewBouncing:bounced completion:nil];
}
- (BOOL)openLeftViewBouncing:(IIViewDeckControllerBlock)bounced completion:(IIViewDeckControllerBlock)completed {
return [self openLeftViewBouncing:bounced callDelegate:YES completion:completed];
}
- (BOOL)openLeftViewBouncing:(IIViewDeckControllerBlock)bounced callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
return [self openLeftViewBouncing:bounced options:UIViewAnimationOptionCurveEaseInOut callDelegate:YES completion:completed];
}
- (BOOL)openLeftViewBouncing:(IIViewDeckControllerBlock)bounced options:(UIViewAnimationOptions)options callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
if (!self.leftController || II_FLOAT_EQUAL(CGRectGetMinX(self.slidingControllerView.frame), self.leftLedge)) return YES;
// check the delegate to allow opening
if (callDelegate && ![self checkDelegate:@selector(viewDeckControllerWillOpenLeftView:animated:) animated:YES]) return NO;
// also close the right view if it's open. Since the delegate can cancel the close, check the result.
if (callDelegate && ![self closeRightViewAnimated:YES options:options callDelegate:callDelegate completion:completed]) return NO;
// first open the view completely, run the block (to allow changes)
[UIView animateWithDuration:OPEN_SLIDE_DURATION(YES) delay:0 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionLayoutSubviews animations:^{
self.leftController.view.hidden = NO;
[self setSlidingFrameForOffset:self.referenceBounds.size.width];
} completion:^(BOOL finished) {
// run block if it's defined
if (bounced) bounced(self);
[self centerViewHidden];
// now slide the view back to the ledge position
[UIView animateWithDuration:OPEN_SLIDE_DURATION(YES) delay:0 options:options | UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionBeginFromCurrentState animations:^{
[self setSlidingFrameForOffset:self.referenceBounds.size.width - self.leftLedge];
} completion:^(BOOL finished) {
if (completed) completed(self);
if (callDelegate) [self performDelegate:@selector(viewDeckControllerDidOpenLeftView:animated:) animated:YES];
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);
}];
}];
return YES;
}
- (BOOL)closeLeftViewAnimated:(BOOL)animated {
return [self closeLeftViewAnimated:animated completion:nil];
}
- (BOOL)closeLeftViewAnimated:(BOOL)animated completion:(IIViewDeckControllerBlock)completed {
return [self closeLeftViewAnimated:animated callDelegate:YES completion:completed];
}
- (BOOL)closeLeftViewAnimated:(BOOL)animated callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
return [self closeLeftViewAnimated:animated options:UIViewAnimationOptionCurveEaseInOut callDelegate:callDelegate completion:completed];
}
- (BOOL)closeLeftViewAnimated:(BOOL)animated options:(UIViewAnimationOptions)options callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
if (self.leftControllerIsClosed) return YES;
// check the delegate to allow closing
if (callDelegate && ![self checkDelegate:@selector(viewDeckControllerWillCloseLeftView:animated:) animated:animated]) return NO;
[UIView animateWithDuration:CLOSE_SLIDE_DURATION(animated) delay:0 options:options | UIViewAnimationOptionLayoutSubviews animations:^{
[self setSlidingFrameForOffset:0];
[self centerViewVisible];
} completion:^(BOOL finished) {
[self hideAppropriateSideViews];
if (completed) completed(self);
if (callDelegate) {
[self performDelegate:@selector(viewDeckControllerDidCloseLeftView:animated:) animated:animated];
[self performDelegate:@selector(viewDeckControllerDidShowCenterView:animated:) animated:animated];
}
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);
}];
return YES;
}
- (BOOL)closeLeftViewBouncing:(IIViewDeckControllerBlock)bounced {
return [self closeLeftViewBouncing:bounced completion:nil];
}
- (BOOL)closeLeftViewBouncing:(IIViewDeckControllerBlock)bounced completion:(IIViewDeckControllerBlock)completed {
return [self closeLeftViewBouncing:bounced callDelegate:YES completion:completed];
}
- (BOOL)closeLeftViewBouncing:(IIViewDeckControllerBlock)bounced callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
if (self.leftControllerIsClosed) return YES;
// check the delegate to allow closing
if (callDelegate && ![self checkDelegate:@selector(viewDeckControllerWillCloseLeftView:animated:) animated:YES]) return NO;
// first open the view completely, run the block (to allow changes) and close it again.
[UIView animateWithDuration:OPEN_SLIDE_DURATION(YES) delay:0 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionLayoutSubviews animations:^{
[self setSlidingFrameForOffset:self.referenceBounds.size.width];
} completion:^(BOOL finished) {
// run block if it's defined
if (bounced) bounced(self);
if (callDelegate && self.delegate && [self.delegate respondsToSelector:@selector(viewDeckController:didBounceWithClosingController:)])
[self.delegate viewDeckController:self didBounceWithClosingController:self.leftController];
[UIView animateWithDuration:CLOSE_SLIDE_DURATION(YES) delay:0 options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionLayoutSubviews animations:^{
[self setSlidingFrameForOffset:0];
[self centerViewVisible];
} completion:^(BOOL finished2) {
[self hideAppropriateSideViews];
if (completed) completed(self);
if (callDelegate) {
[self performDelegate:@selector(viewDeckControllerDidCloseLeftView:animated:) animated:YES];
[self performDelegate:@selector(viewDeckControllerDidShowCenterView:animated:) animated:YES];
}
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);
}];
}];
return YES;
}
- (BOOL)bounceRightView {
return [self bounceRightViewWithCompletion:nil];
}
- (BOOL)bounceRightViewWithCompletion:(IIViewDeckControllerBlock)completed {
return [self bounceRightViewToDistance:40.0f duration:1.2f callDelegate:YES completion:completed];
}