- Fixed menu rotation issue when app launches in landscape mode

This commit is contained in:
Aryan Ghassemi 2014-01-22 07:16:44 -08:00
parent 17c29fcdfa
commit d7ad0e1a34
2 changed files with 38 additions and 14 deletions

View File

@ -45,7 +45,7 @@
#define MENU_DEFAULT_SLIDE_OFFSET 60
#define MENU_FAST_VELOCITY_FOR_SWIPE_FOLLOW_DIRECTION 1200
#define MENU_REVEAL_ANIMATION_DEFAULT_SLIDE_MOVEMENT 100
#define MENU_REVEAL_ANIMATION_DEFAULT_FADE_MAXIMUM_ALPHA .7
#define MENU_REVEAL_ANIMATION_DEFAULT_FADE_MAXIMUM_ALPHA .8
#define STATUS_BAR_HEIGHT 20
static SlideNavigationController *singletonInstance;
@ -111,13 +111,18 @@ static SlideNavigationController *singletonInstance;
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Update shadow size
self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
// Update rotation animation
[self updateMenuFrameAndTransformAccordingToOrientation];
// Avoid an ugnly shadow in background while rotating
self.view.layer.shadowOpacity = 0;
}
@ -126,6 +131,14 @@ static SlideNavigationController *singletonInstance;
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
// we set shadowOpacity to 0 in willRotateToInterfaceOrientation, after the rotation we want to add the shadow back
self.view.layer.shadowOpacity = MENU_SHADOW_OPACITY;
}
- (void)updateMenuFrameAndTransformAccordingToOrientation
{
CGAffineTransform transform = self.view.transform;
self.leftMenu.view.transform = transform;
self.rightMenu.view.transform = transform;
@ -137,11 +150,6 @@ static SlideNavigationController *singletonInstance;
// Move menus accordingly to avoid a weird animation during opening/closing menu after a rotation
[self updateMenuAnimation:MenuLeft];
[self updateMenuAnimation:MenuRight];
self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
// we set shadowOpacity to 0 in willRotateToInterfaceOrientation, after the rotation we want to add the shadow back
self.view.layer.shadowOpacity = MENU_SHADOW_OPACITY;
}
#pragma mark - Public Methods -
@ -311,7 +319,7 @@ static SlideNavigationController *singletonInstance;
{
[self.topViewController.view addGestureRecognizer:self.tapRecognizer];
[self prepareMenuForReveal:menu];
[self prepareMenuForReveal:menu forcePrepare:NO];
[UIView animateWithDuration:duration
delay:0
@ -427,11 +435,17 @@ static SlideNavigationController *singletonInstance;
}
}
- (void)prepareMenuForReveal:(Menu)menu
- (void)prepareMenuForReveal:(Menu)menu forcePrepare:(BOOL)forcePrepare
{
// If menu is already open don't prepare, unless forcePrepare is set to true
if ([self isMenuOpen] && !forcePrepare)
return;
UIViewController *menuViewController = (menu == MenuLeft) ? self.leftMenu : self.rightMenu;
UIViewController *removingMenuViewController = (menu == MenuLeft) ? self.rightMenu : self.leftMenu;
[self updateMenuFrameAndTransformAccordingToOrientation];
// If already has been added to the view (has superview) it means it has been initialized so avoid reinitializing
if (menuViewController.view.superview)
return;
@ -562,15 +576,25 @@ static SlideNavigationController *singletonInstance;
CGPoint velocity = [aPanRecognizer velocityInView:aPanRecognizer.view];
NSInteger movement = translation.x - self.draggingPoint.x;
[self prepareMenuForReveal:(self.horizontalLocation > 0 || (self.horizontalLocation == 0 && translation.x > 0) ) ? MenuLeft : MenuRight];
Menu menu = (self.horizontalLocation > 0 || (self.horizontalLocation == 0 && translation.x > 0) ) ? MenuLeft : MenuRight;
if (aPanRecognizer.state == UIGestureRecognizerStateBegan)
{
[self prepareMenuForReveal:menu forcePrepare:YES];
self.draggingPoint = translation;
}
else if (aPanRecognizer.state == UIGestureRecognizerStateChanged)
{
NSInteger newHorizontalLocation = [self horizontalLocation];
static CGFloat lastHorizontalLocation = 0;
CGFloat newHorizontalLocation = [self horizontalLocation];
// Force prepare menu when slides quickly between left and right menu
if ((lastHorizontalLocation < 0 && newHorizontalLocation > 0) ||
(lastHorizontalLocation > 0 && newHorizontalLocation < 0))
[self prepareMenuForReveal:menu forcePrepare:YES];
lastHorizontalLocation = newHorizontalLocation;
newHorizontalLocation += movement;
if (newHorizontalLocation >= self.minXForDragging && newHorizontalLocation <= self.maxXForDragging)

View File

@ -14,8 +14,8 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="T7T-Ke-Ksy">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<rect key="frame" x="0.0" y="20" width="320" height="548"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES" heightSizable="YES"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="rightMenuCell" textLabel="69X-Zn-twq" style="IBUITableViewCellStyleDefault" id="Pb7-aS-2IS">
@ -130,7 +130,7 @@
<navigationItem key="navigationItem" title="No Menu" id="k71-Cl-mOP">
<barButtonItem key="rightBarButtonItem" id="NgO-HE-8LY">
<segmentedControl key="customView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="bar" selectedSegmentIndex="0" id="nE8-Dp-UZR">
<rect key="frame" x="209" y="6" width="95" height="32"/>
<rect key="frame" x="209" y="8" width="95" height="29"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<segments>
<segment title="Seg 1"/>