Skip to content

Commit

Permalink
Fixed issue where sometimes when swiping really quick it's possible t…
Browse files Browse the repository at this point in the history
…o see the wrong menu
  • Loading branch information
aryaxt authored and Aryan committed Jun 22, 2014
1 parent 2a6be6a commit 437270a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
4 changes: 2 additions & 2 deletions SlideMenu/Source/SlideNavigationController.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
@end

typedef enum{
MenuLeft,
MenuRight,
MenuLeft = 1,
MenuRight = 2
}Menu;

@protocol SlideNavigationContorllerAnimator;
Expand Down
48 changes: 23 additions & 25 deletions SlideMenu/Source/SlideNavigationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ @interface SlideNavigationController() <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
@property (nonatomic, strong) UIPanGestureRecognizer *panRecognizer;
@property (nonatomic, assign) CGPoint draggingPoint;
@property (nonatomic, assign) Menu lastRevealedMenu;
@end

@implementation SlideNavigationController
Expand Down Expand Up @@ -147,7 +148,7 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO

- (void)bounceMenu:(Menu)menu withCompletion:(void (^)())completion
{
[self prepareMenuForReveal:menu forcePrepare:YES];
[self prepareMenuForReveal:menu];
NSInteger movementDirection = (menu == MenuLeft) ? 1 : -1;

[UIView animateWithDuration:.16 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
Expand Down Expand Up @@ -446,8 +447,8 @@ - (BOOL)shouldDisplayMenu:(Menu)menu forViewController:(UIViewController *)vc
- (void)openMenu:(Menu)menu withDuration:(float)duration andCompletion:(void (^)())completion
{
[self enableTapGestureToCloseMenu:YES];
[self prepareMenuForReveal:menu forcePrepare:NO];

[self prepareMenuForReveal:menu];

[UIView animateWithDuration:duration
delay:0
Expand Down Expand Up @@ -542,14 +543,16 @@ - (CGRect)initialRectForMenu
return rect;
}

- (void)prepareMenuForReveal:(Menu)menu forcePrepare:(BOOL)forcePrepare
- (void)prepareMenuForReveal:(Menu)menu
{
UIViewController *menuViewController = (menu == MenuLeft) ? self.leftMenu : self.rightMenu;
// Only prepare menu if it has changed (ex: from MenuLeft to MenuRight or vice versa)
if (self.lastRevealedMenu && menu == self.lastRevealedMenu)
return;

UIViewController *menuViewController = (menu == MenuLeft) ? self.leftMenu : self.rightMenu;
UIViewController *removingMenuViewController = (menu == MenuLeft) ? self.rightMenu : self.leftMenu;

// If menu is already open don't prepare, unless forcePrepare is set to true
if ([self isMenuOpen] && !forcePrepare)
return;

self.lastRevealedMenu = menu;

[removingMenuViewController.view removeFromSuperview];
[self.view.window insertSubview:menuViewController.view atIndex:0];
Expand Down Expand Up @@ -658,35 +661,32 @@ - (void)panDetected:(UIPanGestureRecognizer *)aPanRecognizer
CGPoint velocity = [aPanRecognizer velocityInView:aPanRecognizer.view];
NSInteger movement = translation.x - self.draggingPoint.x;

static Menu lastMenu;
Menu menu = (self.horizontalLocation > 0 || (self.horizontalLocation == 0 && movement > 0) ) ? MenuLeft : MenuRight;

Menu currentMenu;

if (self.horizontalLocation > 0)
currentMenu = MenuLeft;
else if (self.horizontalLocation < 0)
currentMenu = MenuRight;
else
currentMenu = (translation.x > 0) ? MenuLeft : MenuRight;

[self prepareMenuForReveal:currentMenu];

if (aPanRecognizer.state == UIGestureRecognizerStateBegan)
{
if (![self isMenuOpen])
[self prepareMenuForReveal:menu forcePrepare:YES];

self.draggingPoint = translation;
lastMenu = menu;
}
else if (aPanRecognizer.state == UIGestureRecognizerStateChanged)
{
static CGFloat lastHorizontalLocation = 0;
CGFloat newHorizontalLocation = [self horizontalLocation];

// Force prepare menu when slides quickly between left and right menu
if (lastMenu != menu)
[self prepareMenuForReveal:menu forcePrepare:YES];

lastHorizontalLocation = newHorizontalLocation;

newHorizontalLocation += movement;

if (newHorizontalLocation >= self.minXForDragging && newHorizontalLocation <= self.maxXForDragging)
[self moveHorizontallyToLocation:newHorizontalLocation];

self.draggingPoint = translation;
lastMenu = menu;
}
else if (aPanRecognizer.state == UIGestureRecognizerStateEnded)
{
Expand Down Expand Up @@ -733,8 +733,6 @@ - (void)panDetected:(UIPanGestureRecognizer *)aPanRecognizer
else
[self openMenu:(currentX > 0) ? MenuLeft : MenuRight withCompletion:nil];
}

lastMenu = -1;
}
}

Expand Down

0 comments on commit 437270a

Please sign in to comment.