Skip to content

Commit 373be13

Browse files
committed
- Added scale animation
1 parent 7fa9e10 commit 373be13

File tree

4 files changed

+133
-38
lines changed

4 files changed

+133
-38
lines changed

SlideMenu/Helper Classes/MenuViewController.m

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ @implementation MenuViewController
1515

1616
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
1717
{
18-
return 15;
18+
return 10;
1919
}
2020

2121
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
@@ -40,8 +40,28 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
4040
cell.textLabel.text = @"Sign Out";
4141
break;
4242

43-
default:
44-
cell.textLabel.text = @"Random Cell";
43+
case 4:
44+
cell.textLabel.text = @"No Animation";
45+
break;
46+
47+
case 5:
48+
cell.textLabel.text = @"Slide Animation";
49+
break;
50+
51+
case 6:
52+
cell.textLabel.text = @"Fade Animation";
53+
break;
54+
55+
case 7:
56+
cell.textLabel.text = @"Slide And Fade Animation";
57+
break;
58+
59+
case 8:
60+
cell.textLabel.text = @"Scale Animation";
61+
break;
62+
63+
case 9:
64+
cell.textLabel.text = @"Scale And Fade Animation";
4565
break;
4666
}
4767

@@ -54,6 +74,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
5474
bundle: nil];
5575

5676
UIViewController *vc ;
77+
MenuRevealAnimation revealAnimation = MenuRevealAnimationNone;
5778

5879
switch (indexPath.row)
5980
{
@@ -74,11 +95,40 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
7495
return;
7596
break;
7697

98+
case 4:
99+
revealAnimation = MenuRevealAnimationNone;
100+
break;
101+
102+
case 5:
103+
revealAnimation = MenuRevealAnimationSlide;
104+
break;
105+
106+
case 6:
107+
revealAnimation = MenuRevealAnimationFade;
108+
break;
109+
110+
case 7:
111+
revealAnimation = MenuRevealAnimationSlideAndFade;
112+
break;
113+
114+
case 8:
115+
revealAnimation = MenuRevealAnimationScale;
116+
break;
117+
118+
case 9:
119+
revealAnimation = MenuRevealAnimationScaleAndFade;
120+
break;
121+
77122
default:
78123
return;
79124
}
80125

81-
[[SlideNavigationController sharedInstance] switchToViewController:vc withCompletion:nil];
126+
if (vc)
127+
[[SlideNavigationController sharedInstance] switchToViewController:vc withCompletion:nil];
128+
else
129+
[[SlideNavigationController sharedInstance] closeMenuWithCompletion:^{
130+
[SlideNavigationController sharedInstance].menuRevealAnimation = revealAnimation;
131+
}];
82132
}
83133

84134
@end

SlideMenu/Source/SlideNavigationController.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ typedef enum{
4343
MenuRevealAnimationNone,
4444
MenuRevealAnimationFade,
4545
MenuRevealAnimationSlide,
46-
MenuRevealAnimationSlideAndFade
46+
MenuRevealAnimationSlideAndFade,
47+
MenuRevealAnimationScale,
48+
MenuRevealAnimationScaleAndFade
4749
}MenuRevealAnimation;
4850

4951
@interface SlideNavigationController : UINavigationController <UINavigationControllerDelegate>
@@ -60,6 +62,7 @@ typedef enum{
6062
@property (nonatomic, assign) CGFloat menuRevealAnimationFadeMaximumAlpha;
6163
@property (nonatomic, strong) UIColor *menuRevealAnimationFadeColor;
6264
@property (nonatomic, assign) CGFloat menuRevealAnimationSlideMovement;
65+
@property (nonatomic, assign) CGFloat menuRevealAnimationScaleMinScale;
6366

6467
+ (SlideNavigationController *)sharedInstance;
6568
- (void)switchToViewController:(UIViewController *)viewController withCompletion:(void (^)())completion;

SlideMenu/Source/SlideNavigationController.m

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ @implementation SlideNavigationController
4646
#define MENU_FAST_VELOCITY_FOR_SWIPE_FOLLOW_DIRECTION 1200
4747
#define MENU_REVEAL_ANIMATION_DEFAULT_SLIDE_MOVEMENT 100
4848
#define MENU_REVEAL_ANIMATION_DEFAULT_FADE_MAXIMUM_ALPHA .8
49+
#define MENU_REVEAL_ANIMATION_DEFAULT_SCALE_MINIMUM_SCALE .85
4950
#define STATUS_BAR_HEIGHT 20
5051

5152
static SlideNavigationController *singletonInstance;
@@ -90,6 +91,7 @@ - (id)initWithRootViewController:(UIViewController *)rootViewController
9091
- (void)setup
9192
{
9293
self.menuRevealAnimationSlideMovement = MENU_REVEAL_ANIMATION_DEFAULT_SLIDE_MOVEMENT;
94+
self.menuRevealAnimationScaleMinScale = MENU_REVEAL_ANIMATION_DEFAULT_SCALE_MINIMUM_SCALE;
9395
self.menuRevealAnimationFadeMaximumAlpha = MENU_REVEAL_ANIMATION_DEFAULT_FADE_MAXIMUM_ALPHA;
9496
self.menuRevealAnimation = MenuRevealAnimationSlideAndFade;
9597
self.landscapeSlideOffset = MENU_DEFAULT_SLIDE_OFFSET;
@@ -106,6 +108,7 @@ - (void)setup
106108
self.view.layer.rasterizationScale = [UIScreen mainScreen].scale;
107109

108110
[self setEnableSwipeGesture:YES];
111+
[self updateMenuFrameAndTransformAccordingToOrientation];
109112
}
110113

111114
- (void)viewWillLayoutSubviews
@@ -120,9 +123,6 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie
120123
{
121124
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
122125

123-
// Update rotation animation
124-
[self updateMenuFrameAndTransformAccordingToOrientation];
125-
126126
// Avoid an ugnly shadow in background while rotating
127127
self.view.layer.shadowOpacity = 0;
128128
}
@@ -131,6 +131,9 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO
131131
{
132132
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
133133

134+
// Update rotation animation
135+
[self updateMenuFrameAndTransformAccordingToOrientation];
136+
134137
self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
135138

136139
// we set shadowOpacity to 0 in willRotateToInterfaceOrientation, after the rotation we want to add the shadow back
@@ -139,17 +142,13 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO
139142

140143
- (void)updateMenuFrameAndTransformAccordingToOrientation
141144
{
145+
// Animate rotatation when menu is open and device rotates
142146
CGAffineTransform transform = self.view.transform;
143147
self.leftMenu.view.transform = transform;
144148
self.rightMenu.view.transform = transform;
145149

146-
CGRect rect = self.view.frame;
147-
self.leftMenu.view.frame = rect;
148-
self.rightMenu.view.frame = rect;
149-
150-
// Move menus accordingly to avoid a weird animation during opening/closing menu after a rotation
151-
[self updateMenuAnimation:MenuLeft];
152-
[self updateMenuAnimation:MenuRight];
150+
self.leftMenu.view.frame = [self initialRectForMenu];
151+
self.rightMenu.view.frame = [self initialRectForMenu];
153152
}
154153

155154
#pragma mark - Public Methods -
@@ -386,14 +385,17 @@ - (void)updateMenuAnimation:(Menu)menu
386385
? (self.horizontalLocation / self.maxXForDragging)
387386
: (self.horizontalLocation / self.minXForDragging);
388387

389-
if (self.menuRevealAnimation == MenuRevealAnimationFade || self.menuRevealAnimation == MenuRevealAnimationSlideAndFade)
388+
if (self.menuRevealAnimation == MenuRevealAnimationFade ||
389+
self.menuRevealAnimation == MenuRevealAnimationSlideAndFade ||
390+
self.menuRevealAnimation == MenuRevealAnimationScaleAndFade)
390391
{
391392
self.menuRevealFadeAnimationView.frame = menuViewController.view.bounds;
392393
[menuViewController.view addSubview:self.menuRevealFadeAnimationView];
393394
self.menuRevealFadeAnimationView.alpha = self.menuRevealAnimationFadeMaximumAlpha - (self.menuRevealAnimationFadeMaximumAlpha *progress);
394395
}
395396

396-
if (self.menuRevealAnimation == MenuRevealAnimationSlide || self.menuRevealAnimation == MenuRevealAnimationSlideAndFade)
397+
if (self.menuRevealAnimation == MenuRevealAnimationSlide ||
398+
self.menuRevealAnimation == MenuRevealAnimationSlideAndFade)
397399
{
398400
NSInteger location = (menu == MenuLeft)
399401
? (self.menuRevealAnimationSlideMovement * -1) + (self.menuRevealAnimationSlideMovement * progress)
@@ -405,34 +407,56 @@ - (void)updateMenuAnimation:(Menu)menu
405407
if (menu == MenuRight)
406408
location = (location < 0) ? 0 : location;
407409

408-
CGRect rect = menuViewController.view.frame;
409-
BOOL isIos7 = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0");
410+
CGRect rect = [self initialRectForMenu];
410411

411412
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
412413
{
413414
rect.origin.y = (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) ? location : location*-1;
414-
415-
if (!isIos7)
416-
{
417-
// For some reasons in landscape belos the status bar is considered y=0, but in portrait it's considered y=20
418-
rect.origin.x = (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) ? 0 : STATUS_BAR_HEIGHT;
419-
rect.size.width = self.view.frame.size.width-STATUS_BAR_HEIGHT;
420-
}
421415
}
422416
else
423417
{
424418
rect.origin.x = (self.interfaceOrientation == UIInterfaceOrientationPortrait) ? location : location*-1;
425-
426-
if (!isIos7)
427-
{
428-
// For some reasons in landscape belos the status bar is considered y=0, but in portrait it's considered y=20
429-
rect.origin.y = (self.interfaceOrientation == UIInterfaceOrientationPortrait) ? STATUS_BAR_HEIGHT : 0;
430-
rect.size.height = self.view.frame.size.height-STATUS_BAR_HEIGHT;
431-
}
432419
}
433420

434421
menuViewController.view.frame = rect;
435422
}
423+
424+
if (self.menuRevealAnimation == MenuRevealAnimationScale ||
425+
self.menuRevealAnimation == MenuRevealAnimationScaleAndFade)
426+
{
427+
CGFloat scale = MIN(1, (1-self.menuRevealAnimationScaleMinScale) *progress + self.menuRevealAnimationScaleMinScale);
428+
menuViewController.view.transform = CGAffineTransformScale(self.view.transform, scale, scale);
429+
}
430+
}
431+
432+
- (CGRect)initialRectForMenu
433+
{
434+
CGRect rect = self.view.frame;
435+
rect.origin.x = 0;
436+
rect.origin.y = 0;
437+
438+
BOOL isIos7 = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0");
439+
440+
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
441+
{
442+
if (!isIos7)
443+
{
444+
// For some reasons in landscape belos the status bar is considered y=0, but in portrait it's considered y=20
445+
rect.origin.x = (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) ? 0 : STATUS_BAR_HEIGHT;
446+
rect.size.width = self.view.frame.size.width-STATUS_BAR_HEIGHT;
447+
}
448+
}
449+
else
450+
{
451+
if (!isIos7)
452+
{
453+
// For some reasons in landscape belos the status bar is considered y=0, but in portrait it's considered y=20
454+
rect.origin.y = (self.interfaceOrientation == UIInterfaceOrientationPortrait) ? STATUS_BAR_HEIGHT : 0;
455+
rect.size.height = self.view.frame.size.height-STATUS_BAR_HEIGHT;
456+
}
457+
}
458+
459+
return rect;
436460
}
437461

438462
- (void)prepareMenuForReveal:(Menu)menu forcePrepare:(BOOL)forcePrepare
@@ -444,19 +468,22 @@ - (void)prepareMenuForReveal:(Menu)menu forcePrepare:(BOOL)forcePrepare
444468
UIViewController *menuViewController = (menu == MenuLeft) ? self.leftMenu : self.rightMenu;
445469
UIViewController *removingMenuViewController = (menu == MenuLeft) ? self.rightMenu : self.leftMenu;
446470

447-
[self updateMenuFrameAndTransformAccordingToOrientation];
471+
//[self updateMenuFrameAndTransformAccordingToOrientation];
448472

449473
// If already has been added to the view (has superview) it means it has been initialized so avoid reinitializing
450474
if (menuViewController.view.superview)
451475
return;
452476

453-
if (self.menuRevealAnimation == MenuRevealAnimationFade || self.menuRevealAnimation == MenuRevealAnimationSlideAndFade)
477+
if (self.menuRevealAnimation == MenuRevealAnimationFade ||
478+
self.menuRevealAnimation == MenuRevealAnimationSlideAndFade ||
479+
self.menuRevealAnimation == MenuRevealAnimationScaleAndFade)
454480
{
455481
self.menuRevealFadeAnimationView.alpha = self.menuRevealAnimationFadeMaximumAlpha;
456482
self.menuRevealFadeAnimationView.frame = menuViewController.view.bounds;
457483
}
458484

459-
if (self.menuRevealAnimation == MenuRevealAnimationSlide || self.menuRevealAnimation == MenuRevealAnimationSlideAndFade)
485+
if (self.menuRevealAnimation == MenuRevealAnimationSlide ||
486+
self.menuRevealAnimation == MenuRevealAnimationSlideAndFade)
460487
{
461488
CGRect rect = menuViewController.view.frame;
462489

@@ -486,6 +513,12 @@ - (void)prepareMenuForReveal:(Menu)menu forcePrepare:(BOOL)forcePrepare
486513
menuViewController.view.frame = rect;
487514
}
488515

516+
if (self.menuRevealAnimation == MenuRevealAnimationScale ||
517+
self.menuRevealAnimation == MenuRevealAnimationScaleAndFade)
518+
{
519+
menuViewController.view.transform = CGAffineTransformScale(self.view.transform, self.menuRevealAnimationScaleMinScale, self.menuRevealAnimationScaleMinScale);
520+
}
521+
489522
[removingMenuViewController.view removeFromSuperview];
490523
[self.view.window insertSubview:menuViewController.view atIndex:0];
491524
}
@@ -581,7 +614,9 @@ - (void)panDetected:(UIPanGestureRecognizer *)aPanRecognizer
581614

582615
if (aPanRecognizer.state == UIGestureRecognizerStateBegan)
583616
{
584-
[self prepareMenuForReveal:menu forcePrepare:YES];
617+
if (![self isMenuOpen])
618+
[self prepareMenuForReveal:menu forcePrepare:YES];
619+
585620
self.draggingPoint = translation;
586621
lastMenu = menu;
587622
}
@@ -728,4 +763,11 @@ - (void)setMenuRevealAnimationFadeColor:(UIColor *)menuRevealAnimationFadeColor
728763
self.menuRevealFadeAnimationView.backgroundColor = menuRevealAnimationFadeColor;
729764
}
730765

766+
- (void)setMenuRevealAnimation:(MenuRevealAnimation)menuRevealAnimation
767+
{
768+
_menuRevealAnimation = menuRevealAnimation;
769+
770+
[self updateMenuFrameAndTransformAccordingToOrientation];
771+
}
772+
731773
@end

SlideMenu/en.lproj/MainStoryboard_iPhone.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
1515
<subviews>
1616
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="T7T-Ke-Ksy">
17-
<rect key="frame" x="0.0" y="20" width="320" height="548"/>
17+
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
1818
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES" heightSizable="YES"/>
1919
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
2020
<prototypes>

0 commit comments

Comments
 (0)