-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWRevealViewController.m
executable file
·1936 lines (1462 loc) · 66.4 KB
/
SWRevealViewController.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
/*
Copyright (c) 2013 Joan Lluch <[email protected]>
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.
Early code inspired on a similar class by Philip Kluz ([email protected])
*/
#import <QuartzCore/QuartzCore.h>
#import "SWRevealViewController.h"
#pragma mark - StatusBar Helper Function
// computes the required offset adjustment due to the status bar for the passed in view,
// it will return the statusBar height if view fully overlaps the statusBar, otherwise returns 0.0f
static CGFloat statusBarAdjustment( UIView* view )
{
CGFloat adjustment = 0.0f;
UIApplication *app = [UIApplication sharedApplication];
CGRect viewFrame = [view convertRect:view.bounds toView:[app keyWindow]];
CGRect statusBarFrame = [app statusBarFrame];
if ( CGRectIntersectsRect(viewFrame, statusBarFrame) )
adjustment = fminf(statusBarFrame.size.width, statusBarFrame.size.height);
return adjustment;
}
#pragma mark - SWRevealView Class
@interface SWRevealView: UIView
{
__weak SWRevealViewController *_c;
}
@property (nonatomic, readonly) UIView *rearView;
@property (nonatomic, readonly) UIView *rightView;
@property (nonatomic, readonly) UIView *frontView;
@property (nonatomic, assign) BOOL disableLayout;
@end
@interface SWRevealViewController()
- (void)_getRevealWidth:(CGFloat*)pRevealWidth revealOverDraw:(CGFloat*)pRevealOverdraw forSymetry:(int)symetry;
- (void)_getBounceBack:(BOOL*)pBounceBack pStableDrag:(BOOL*)pStableDrag forSymetry:(int)symetry;
- (void)_getAdjustedFrontViewPosition:(FrontViewPosition*)frontViewPosition forSymetry:(int)symetry;
@end
@implementation SWRevealView
static CGFloat scaledValue( CGFloat v1, CGFloat min2, CGFloat max2, CGFloat min1, CGFloat max1)
{
CGFloat result = min2 + (v1-min1)*((max2-min2)/(max1-min1));
if ( result != result ) return min2; // nan
if ( result < min2 ) return min2;
if ( result > max2 ) return max2;
return result;
}
- (id)initWithFrame:(CGRect)frame controller:(SWRevealViewController*)controller
{
self = [super initWithFrame:frame];
if ( self )
{
_c = controller;
CGRect bounds = self.bounds;
_frontView = [[UIView alloc] initWithFrame:bounds];
_frontView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self reloadShadow];
[self addSubview:_frontView];
}
return self;
}
- (void)reloadShadow
{
CALayer *frontViewLayer = _frontView.layer;
frontViewLayer.shadowColor = [_c.frontViewShadowColor CGColor];
frontViewLayer.shadowOpacity = _c.frontViewShadowOpacity;
frontViewLayer.shadowOffset = _c.frontViewShadowOffset;
frontViewLayer.shadowRadius = _c.frontViewShadowRadius;
}
- (CGRect)hierarchycalFrameAdjustment:(CGRect)frame
{
if ( _c.presentFrontViewHierarchically )
{
UINavigationBar *dummyBar = [[UINavigationBar alloc] init];
CGFloat barHeight = [dummyBar sizeThatFits:CGSizeMake(100,100)].height;
CGFloat offset = barHeight + statusBarAdjustment(self);
frame.origin.y += offset;
frame.size.height -= offset;
}
return frame;
}
- (void)prepareRearViewForPosition:(FrontViewPosition)newPosition
{
if ( _rearView == nil )
{
_rearView = [[UIView alloc] initWithFrame:self.bounds];
_rearView.autoresizingMask = /*UIViewAutoresizingFlexibleWidth|*/UIViewAutoresizingFlexibleHeight;
[self insertSubview:_rearView belowSubview:_frontView];
}
CGFloat xLocation = [self frontLocationForPosition:_c.frontViewPosition];
[self _layoutRearViewsForLocation:xLocation];
[self _prepareForNewPosition:newPosition];
}
- (void)prepareRightViewForPosition:(FrontViewPosition)newPosition
{
if ( _rightView == nil )
{
_rightView = [[UIView alloc] initWithFrame:self.bounds];
_rightView.autoresizingMask = /*UIViewAutoresizingFlexibleWidth|*/UIViewAutoresizingFlexibleHeight;
[self insertSubview:_rightView belowSubview:_frontView];
}
CGFloat xLocation = [self frontLocationForPosition:_c.frontViewPosition];
[self _layoutRearViewsForLocation:xLocation];
[self _prepareForNewPosition:newPosition];
}
- (void)unloadRearView
{
[_rearView removeFromSuperview];
_rearView = nil;
}
- (void)unloadRightView
{
[_rightView removeFromSuperview];
_rightView = nil;
}
- (CGFloat)frontLocationForPosition:(FrontViewPosition)frontViewPosition
{
CGFloat revealWidth;
CGFloat revealOverdraw;
CGFloat location = 0.0f;
int symetry = frontViewPosition<FrontViewPositionLeft? -1 : 1;
[_c _getRevealWidth:&revealWidth revealOverDraw:&revealOverdraw forSymetry:symetry];
[_c _getAdjustedFrontViewPosition:&frontViewPosition forSymetry:symetry];
if ( frontViewPosition == FrontViewPositionRight )
location = revealWidth;
else if ( frontViewPosition > FrontViewPositionRight )
location = revealWidth + revealOverdraw;
return location*symetry;
}
- (void)dragFrontViewToXLocation:(CGFloat)xLocation
{
CGRect bounds = self.bounds;
xLocation = [self _adjustedDragLocationForLocation:xLocation];
[self _layoutRearViewsForLocation:xLocation];
CGRect frame = CGRectMake(xLocation, 0.0f, bounds.size.width, bounds.size.height);
_frontView.frame = [self hierarchycalFrameAdjustment:frame];
}
# pragma mark - overrides
- (void)layoutSubviews
{
if ( _disableLayout ) return;
CGRect bounds = self.bounds;
FrontViewPosition position = _c.frontViewPosition;
CGFloat xLocation = [self frontLocationForPosition:position];
// set rear view frames
[self _layoutRearViewsForLocation:xLocation];
// set front view frame
CGRect frame = CGRectMake(xLocation, 0.0f, bounds.size.width, bounds.size.height);
_frontView.frame = [self hierarchycalFrameAdjustment:frame];
// setup front view shadow path if needed (front view loaded and not removed)
UIViewController *frontViewController = _c.frontViewController;
BOOL viewLoaded = frontViewController != nil && frontViewController.isViewLoaded;
BOOL viewNotRemoved = position > FrontViewPositionLeftSideMostRemoved && position < FrontViewPositionRightMostRemoved;
CGRect shadowBounds = viewLoaded && viewNotRemoved ? _frontView.bounds : CGRectZero;
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:shadowBounds];
_frontView.layer.shadowPath = shadowPath.CGPath;
}
- (BOOL)pointInsideD:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL isInside = [super pointInside:point withEvent:event];
if ( _c.extendsPointInsideHit )
{
if ( !isInside && _rearView && [_c.rearViewController isViewLoaded] )
{
CGPoint pt = [self convertPoint:point toView:_rearView];
isInside = [_rearView pointInside:pt withEvent:event];
}
if ( !isInside && _frontView && [_c.frontViewController isViewLoaded] )
{
CGPoint pt = [self convertPoint:point toView:_frontView];
isInside = [_frontView pointInside:pt withEvent:event];
}
if ( !isInside && _rightView && [_c.rightViewController isViewLoaded] )
{
CGPoint pt = [self convertPoint:point toView:_rightView];
isInside = [_rightView pointInside:pt withEvent:event];
}
}
return isInside;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL isInside = [super pointInside:point withEvent:event];
if ( !isInside && _c.extendsPointInsideHit )
{
UIView *testViews[] = { _rearView, _frontView, _rightView };
UIViewController *testControllers[] = { _c.rearViewController, _c.frontViewController, _c.rightViewController };
for ( NSInteger i=0 ; i<3 && !isInside ; i++ )
{
if ( testViews[i] && [testControllers[i] isViewLoaded] )
{
CGPoint pt = [self convertPoint:point toView:testViews[i]];
isInside = [testViews[i] pointInside:pt withEvent:event];
}
}
}
return isInside;
}
# pragma mark - private
- (void)_layoutRearViewsForLocation:(CGFloat)xLocation
{
CGRect bounds = self.bounds;
CGFloat rearRevealWidth = _c.rearViewRevealWidth;
if ( rearRevealWidth < 0) rearRevealWidth = bounds.size.width + _c.rearViewRevealWidth;
CGFloat rearXLocation = scaledValue(xLocation, -_c.rearViewRevealDisplacement, 0, 0, rearRevealWidth);
CGFloat rearWidth = rearRevealWidth + _c.rearViewRevealOverdraw;
_rearView.frame = CGRectMake(rearXLocation, 0.0, rearWidth, bounds.size.height);
CGFloat rightRevealWidth = _c.rightViewRevealWidth;
if ( rightRevealWidth < 0) rightRevealWidth = bounds.size.width + _c.rightViewRevealWidth;
CGFloat rightXLocation = scaledValue(xLocation, 0, _c.rightViewRevealDisplacement, -rightRevealWidth, 0);
CGFloat rightWidth = rightRevealWidth + _c.rightViewRevealOverdraw;
_rightView.frame = CGRectMake(bounds.size.width-rightWidth+rightXLocation, 0.0f, rightWidth, bounds.size.height);
}
- (void)_prepareForNewPosition:(FrontViewPosition)newPosition;
{
if ( _rearView == nil || _rightView == nil )
return;
int symetry = newPosition<FrontViewPositionLeft? -1 : 1;
NSArray *subViews = self.subviews;
NSInteger rearIndex = [subViews indexOfObjectIdenticalTo:_rearView];
NSInteger rightIndex = [subViews indexOfObjectIdenticalTo:_rightView];
if ( (symetry < 0 && rightIndex < rearIndex) || (symetry > 0 && rearIndex < rightIndex) )
[self exchangeSubviewAtIndex:rightIndex withSubviewAtIndex:rearIndex];
}
- (CGFloat)_adjustedDragLocationForLocation:(CGFloat)x
{
CGFloat result;
CGFloat revealWidth;
CGFloat revealOverdraw;
BOOL bounceBack;
BOOL stableDrag;
FrontViewPosition position = _c.frontViewPosition;
int symetry = x<0 ? -1 : 1;
[_c _getRevealWidth:&revealWidth revealOverDraw:&revealOverdraw forSymetry:symetry];
[_c _getBounceBack:&bounceBack pStableDrag:&stableDrag forSymetry:symetry];
BOOL stableTrack = !bounceBack || stableDrag || position==FrontViewPositionRightMost || position==FrontViewPositionLeftSideMost;
if ( stableTrack )
{
revealWidth += revealOverdraw;
revealOverdraw = 0.0f;
}
x = x * symetry;
if (x <= revealWidth)
result = x; // Translate linearly.
else if (x <= revealWidth+2*revealOverdraw)
result = revealWidth + (x-revealWidth)/2; // slow down translation by halph the movement.
else
result = revealWidth+revealOverdraw; // keep at the rightMost location.
return result * symetry;
}
@end
#pragma mark - SWContextTransitioningObject
@interface SWContextTransitionObject : NSObject<UIViewControllerContextTransitioning>
@end
@implementation SWContextTransitionObject
{
__weak SWRevealViewController *_revealVC;
UIView *_view;
UIViewController *_toVC;
UIViewController *_fromVC;
void (^_completion)(void);
}
- (id)initWithRevealController:(SWRevealViewController*)revealVC containerView:(UIView*)view fromVC:(UIViewController*)fromVC
toVC:(UIViewController*)toVC completion:(void (^)(void))completion
{
self = [super init];
if ( self )
{
_revealVC = revealVC;
_view = view;
_fromVC = fromVC;
_toVC = toVC;
_completion = completion;
}
return self;
}
- (UIView *)containerView
{
return _view;
}
- (BOOL)isAnimated
{
return YES;
}
- (BOOL)isInteractive
{
return NO; // not supported
}
- (BOOL)transitionWasCancelled
{
return NO; // not supported
}
- (CGAffineTransform)targetTransform
{
return CGAffineTransformIdentity;
}
- (UIModalPresentationStyle)presentationStyle
{
return UIModalPresentationNone; // not applicable
}
- (void)updateInteractiveTransition:(CGFloat)percentComplete
{
// not supported
}
- (void)finishInteractiveTransition
{
// not supported
}
- (void)cancelInteractiveTransition
{
// not supported
}
- (void)completeTransition:(BOOL)didComplete
{
_completion();
}
- (UIViewController *)viewControllerForKey:(NSString *)key
{
if ( [key isEqualToString:UITransitionContextFromViewControllerKey] )
return _fromVC;
if ( [key isEqualToString:UITransitionContextToViewControllerKey] )
return _toVC;
return nil;
}
- (UIView *)viewForKey:(NSString *)key
{
return nil;
}
- (CGRect)initialFrameForViewController:(UIViewController *)vc
{
return _view.bounds;
}
- (CGRect)finalFrameForViewController:(UIViewController *)vc
{
return _view.bounds;
}
@end
#pragma mark - SWDefaultAnimationController Class
@interface SWDefaultAnimationController : NSObject<UIViewControllerAnimatedTransitioning>
@end
@implementation SWDefaultAnimationController
{
NSTimeInterval _duration;
}
- (id)initWithDuration:(NSTimeInterval)duration
{
self = [super init];
if ( self )
{
_duration = duration;
}
return self;
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return _duration;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if ( fromViewController )
{
[UIView transitionFromView:fromViewController.view toView:toViewController.view duration:_duration
options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationOptionOverrideInheritedOptions
completion:^(BOOL finished) { [transitionContext completeTransition:finished]; }];
}
else
{
// tansitionFromView does not correctly handle the case where the fromView is nil (at least on iOS7) it just pops up the toView view with no animation,
// so in such case we replace the crossDissolve animation by a simple alpha animation on the appearing view
UIView *toView = toViewController.view;
CGFloat alpha = toView.alpha;
toView.alpha = 0;
[UIView animateWithDuration:_duration delay:0 options:UIViewAnimationOptionCurveEaseOut
animations:^{ toView.alpha = alpha;}
completion:^(BOOL finished) { [transitionContext completeTransition:finished];}];
}
}
@end
#pragma mark - SWRevealViewControllerPanGestureRecognizer
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface SWRevealViewControllerPanGestureRecognizer : UIPanGestureRecognizer
@end
@implementation SWRevealViewControllerPanGestureRecognizer
{
BOOL _dragging;
CGPoint _beginPoint;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
_beginPoint = [touch locationInView:self.view];
_dragging = NO;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if ( _dragging || self.state == UIGestureRecognizerStateFailed)
return;
const CGFloat kDirectionPanThreshold = 5;
UITouch *touch = [touches anyObject];
CGPoint nowPoint = [touch locationInView:self.view];
if (ABS(nowPoint.x - _beginPoint.x) > kDirectionPanThreshold) _dragging = YES;
else if (ABS(nowPoint.y - _beginPoint.y) > kDirectionPanThreshold) self.state = UIGestureRecognizerStateFailed;
}
@end
#pragma mark - SWRevealViewController Class
@interface SWRevealViewController()<UIGestureRecognizerDelegate>
{
SWRevealView *_contentView;
UIPanGestureRecognizer *_panGestureRecognizer;
UITapGestureRecognizer *_tapGestureRecognizer;
FrontViewPosition _frontViewPosition;
FrontViewPosition _rearViewPosition;
FrontViewPosition _rightViewPosition;
SWContextTransitionObject *_rearTransitioningController;
SWContextTransitionObject *_frontTransitioningController;
SWContextTransitionObject *_rightTransitioningController;
}
@end
@implementation SWRevealViewController
{
FrontViewPosition _panInitialFrontPosition;
NSMutableArray *_animationQueue;
BOOL _userInteractionStore;
}
const int FrontViewPositionNone = 0xff;
#pragma mark - Init
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if ( self )
{
[self _initDefaultProperties];
}
return self;
}
- (id)init
{
return [self initWithRearViewController:nil frontViewController:nil];
}
- (id)initWithRearViewController:(UIViewController *)rearViewController frontViewController:(UIViewController *)frontViewController;
{
self = [super init];
if ( self )
{
[self _initDefaultProperties];
[self _performTransitionOperation:SWRevealControllerOperationReplaceRearController withViewController:rearViewController animated:NO];
[self _performTransitionOperation:SWRevealControllerOperationReplaceFrontController withViewController:frontViewController animated:NO];
}
return self;
}
- (void)_initDefaultProperties
{
_frontViewPosition = FrontViewPositionLeft;
_rearViewPosition = FrontViewPositionLeft;
_rightViewPosition = FrontViewPositionLeft;
_rearViewRevealWidth = 260.0f;
_rearViewRevealOverdraw = 60.0f;
_rearViewRevealDisplacement = 40.0f;
_rightViewRevealWidth = 260.0f;
_rightViewRevealOverdraw = 60.0f;
_rightViewRevealDisplacement = 40.0f;
_bounceBackOnOverdraw = YES;
_bounceBackOnLeftOverdraw = YES;
_stableDragOnOverdraw = NO;
_stableDragOnLeftOverdraw = NO;
_presentFrontViewHierarchically = NO;
_quickFlickVelocity = 250.0f;
_toggleAnimationDuration = 0.3;
_toggleAnimationType = SWRevealToggleAnimationTypeSpring;
_springDampingRatio = 1;
_replaceViewAnimationDuration = 0.25;
_frontViewShadowRadius = 2.5f;
_frontViewShadowOffset = CGSizeMake(0.0f, 2.5f);
_frontViewShadowOpacity = 1.0f;
_frontViewShadowColor = [UIColor blackColor];
_userInteractionStore = YES;
_animationQueue = [NSMutableArray array];
_draggableBorderWidth = 0.0f;
_clipsViewsToBounds = NO;
_extendsPointInsideHit = NO;
}
#pragma mark - StatusBar
- (UIViewController *)childViewControllerForStatusBarStyle
{
int positionDif = _frontViewPosition - FrontViewPositionLeft;
UIViewController *controller = _frontViewController;
if ( positionDif > 0 ) controller = _rearViewController;
else if ( positionDif < 0 ) controller = _rightViewController;
return controller;
}
- (UIViewController *)childViewControllerForStatusBarHidden
{
UIViewController *controller = [self childViewControllerForStatusBarStyle];
return controller;
}
#pragma mark - View lifecycle
- (void)loadView
{
// Do not call super, to prevent the apis from unfruitful looking for inexistent xibs!
//[super loadView];
// load any defined front/rear controllers from the storyboard before
[self loadStoryboardControllers];
// This is what Apple used to tell us to set as the initial frame, which is of course totally irrelevant
// with view controller containment patterns, let's leave it for the sake of it!
// CGRect frame = [[UIScreen mainScreen] applicationFrame];
// On iOS7 the applicationFrame does not return the whole screen. This is possibly a bug.
// As a workaround we use the screen bounds, this still works on iOS6, any zero based frame would work anyway!
CGRect frame = [[UIScreen mainScreen] bounds];
// create a custom content view for the controller
_contentView = [[SWRevealView alloc] initWithFrame:frame controller:self];
// set the content view to resize along with its superview
[_contentView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
// set the content view to clip its bounds if requested
[_contentView setClipsToBounds:_clipsViewsToBounds];
// set our contentView to the controllers view
self.view = _contentView;
// Apple also tells us to do this:
_contentView.backgroundColor = [UIColor blackColor];
// we set the current frontViewPosition to none before seting the
// desired initial position, this will force proper controller reload
FrontViewPosition initialPosition = _frontViewPosition;
_frontViewPosition = FrontViewPositionNone;
_rearViewPosition = FrontViewPositionNone;
_rightViewPosition = FrontViewPositionNone;
// now set the desired initial position
[self _setFrontViewPosition:initialPosition withDuration:0.0];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Uncomment the following code if you want the child controllers
// to be loaded at this point.
//
// We leave this commented out because we think loading childs here is conceptually wrong.
// Instead, we refrain view loads until necesary, for example we may never load
// the rear controller view -or the front controller view- if it is never displayed.
//
// If you need to manipulate views of any of your child controllers in an override
// of this method, you can load yourself the views explicitly on your overriden method.
// However we discourage it as an app following the MVC principles should never need to do so
// [_frontViewController view];
// [_rearViewController view];
// we store at this point the view's user interaction state as we may temporarily disable it
// and resume it back to the previous state, it is possible to override this behaviour by
// intercepting it on the panGestureBegan and panGestureEnded delegates
_userInteractionStore = _contentView.userInteractionEnabled;
}
- (NSUInteger)supportedInterfaceOrientations
{
// we could have simply not implemented this, but we choose to call super to make explicit that we
// want the default behavior.
return [super supportedInterfaceOrientations];
}
#pragma mark - Public methods and property accessors
- (void)setFrontViewController:(UIViewController *)frontViewController
{
[self setFrontViewController:frontViewController animated:NO];
}
- (void)setFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated
{
if ( ![self isViewLoaded])
{
[self _performTransitionOperation:SWRevealControllerOperationReplaceFrontController withViewController:frontViewController animated:NO];
return;
}
[self _dispatchTransitionOperation:SWRevealControllerOperationReplaceFrontController withViewController:frontViewController animated:animated];
}
- (void)pushFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated
{
if ( ![self isViewLoaded])
{
[self _performTransitionOperation:SWRevealControllerOperationReplaceFrontController withViewController:frontViewController animated:NO];
return;
}
[self _dispatchPushFrontViewController:frontViewController animated:animated];
}
- (void)setRearViewController:(UIViewController *)rearViewController
{
[self setRearViewController:rearViewController animated:NO];
}
- (void)setRearViewController:(UIViewController *)rearViewController animated:(BOOL)animated
{
if ( ![self isViewLoaded])
{
[self _performTransitionOperation:SWRevealControllerOperationReplaceRearController withViewController:rearViewController animated:NO];
return;
}
[self _dispatchTransitionOperation:SWRevealControllerOperationReplaceRearController withViewController:rearViewController animated:animated];
}
- (void)setRightViewController:(UIViewController *)rightViewController
{
[self setRightViewController:rightViewController animated:NO];
}
- (void)setRightViewController:(UIViewController *)rightViewController animated:(BOOL)animated
{
if ( ![self isViewLoaded])
{
[self _performTransitionOperation:SWRevealControllerOperationReplaceRightController withViewController:rightViewController animated:NO];
return;
}
[self _dispatchTransitionOperation:SWRevealControllerOperationReplaceRightController withViewController:rightViewController animated:animated];
}
- (void)revealToggleAnimated:(BOOL)animated
{
FrontViewPosition toggledFrontViewPosition = FrontViewPositionLeft;
if (_frontViewPosition <= FrontViewPositionLeft)
toggledFrontViewPosition = FrontViewPositionRight;
[self setFrontViewPosition:toggledFrontViewPosition animated:animated];
}
- (void)rightRevealToggleAnimated:(BOOL)animated
{
FrontViewPosition toggledFrontViewPosition = FrontViewPositionLeft;
if (_frontViewPosition >= FrontViewPositionLeft)
toggledFrontViewPosition = FrontViewPositionLeftSide;
[self setFrontViewPosition:toggledFrontViewPosition animated:animated];
}
- (void)setFrontViewPosition:(FrontViewPosition)frontViewPosition
{
[self setFrontViewPosition:frontViewPosition animated:NO];
}
- (void)setFrontViewPosition:(FrontViewPosition)frontViewPosition animated:(BOOL)animated
{
if ( ![self isViewLoaded] )
{
_frontViewPosition = frontViewPosition;
_rearViewPosition = frontViewPosition;
_rightViewPosition = frontViewPosition;
return;
}
[self _dispatchSetFrontViewPosition:frontViewPosition animated:animated];
}
- (void)setFrontViewShadowRadius:(CGFloat)frontViewShadowRadius
{
_frontViewShadowRadius = frontViewShadowRadius;
[_contentView reloadShadow];
}
- (void)setFrontViewShadowOffset:(CGSize)frontViewShadowOffset
{
_frontViewShadowOffset = frontViewShadowOffset;
[_contentView reloadShadow];
}
- (void)setFrontViewShadowOpacity:(CGFloat)frontViewShadowOpacity
{
_frontViewShadowOpacity = frontViewShadowOpacity;
[_contentView reloadShadow];
}
- (void)setFrontViewShadowColor:(UIColor *)frontViewShadowColor
{
_frontViewShadowColor = frontViewShadowColor;
[_contentView reloadShadow];
}
- (UIPanGestureRecognizer*)panGestureRecognizer
{
if ( _panGestureRecognizer == nil )
{
_panGestureRecognizer = [[SWRevealViewControllerPanGestureRecognizer alloc] initWithTarget:self action:@selector(_handleRevealGesture:)];
_panGestureRecognizer.delegate = self;
[_contentView.frontView addGestureRecognizer:_panGestureRecognizer];
}
return _panGestureRecognizer;
}
- (UITapGestureRecognizer*)tapGestureRecognizer
{
if ( _tapGestureRecognizer == nil )
{
UITapGestureRecognizer *tapRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTapGesture:)];
tapRecognizer.delegate = self;
[_contentView.frontView addGestureRecognizer:tapRecognizer];
_tapGestureRecognizer = tapRecognizer ;
}
return _tapGestureRecognizer;
}
- (void)setClipsViewsToBounds:(BOOL)clipsViewsToBounds
{
_clipsViewsToBounds = clipsViewsToBounds;
[_contentView setClipsToBounds:clipsViewsToBounds];
}
#pragma mark - Provided acction methods
- (IBAction)revealToggle:(id)sender
{
[self revealToggleAnimated:YES];
}
- (IBAction)rightRevealToggle:(id)sender
{
[self rightRevealToggleAnimated:YES];
}
#pragma mark - UserInteractionEnabling
// disable userInteraction on the entire control
- (void)_disableUserInteraction
{
[_contentView setUserInteractionEnabled:NO];
[_contentView setDisableLayout:YES];
}
// restore userInteraction on the control
- (void)_restoreUserInteraction
{
// we use the stored userInteraction state just in case a developer decided
// to have our view interaction disabled beforehand
[_contentView setUserInteractionEnabled:_userInteractionStore];
[_contentView setDisableLayout:NO];
}
#pragma mark - PanGesture progress notification
- (void)_notifyPanGestureBegan
{
if ( [_delegate respondsToSelector:@selector(revealControllerPanGestureBegan:)] )
[_delegate revealControllerPanGestureBegan:self];
CGFloat xLocation, dragProgress, overProgress;
[self _getDragLocation:&xLocation progress:&dragProgress overdrawProgress:&overProgress];
if ( [_delegate respondsToSelector:@selector(revealController:panGestureBeganFromLocation:progress:overProgress:)] )
[_delegate revealController:self panGestureBeganFromLocation:xLocation progress:dragProgress overProgress:overProgress];
else if ( [_delegate respondsToSelector:@selector(revealController:panGestureBeganFromLocation:progress:)] )
[_delegate revealController:self panGestureBeganFromLocation:xLocation progress:dragProgress];
}
- (void)_notifyPanGestureMoved
{
CGFloat xLocation, dragProgress, overProgress;
[self _getDragLocation:&xLocation progress:&dragProgress overdrawProgress:&overProgress];
if ( [_delegate respondsToSelector:@selector(revealController:panGestureMovedToLocation:progress:overProgress:)] )
[_delegate revealController:self panGestureMovedToLocation:xLocation progress:dragProgress overProgress:overProgress];