Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added elastic boolean to let the user pan the center view controller past the edge #114

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MFSideMenu/MFSideMenuContainerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ typedef enum {
@property (nonatomic, assign) BOOL menuSlideAnimationEnabled;
@property (nonatomic, assign) CGFloat menuSlideAnimationFactor; // higher = less menu movement on animation

// allow elastic panning when the side menu is in the open state
@property (nonatomic, assign) BOOL elastic;

- (void)toggleLeftSideMenuCompletion:(void (^)(void))completion;
- (void)toggleRightSideMenuCompletion:(void (^)(void))completion;
Expand Down
75 changes: 55 additions & 20 deletions MFSideMenu/MFSideMenuContainerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ @implementation MFSideMenuContainerViewController
@synthesize menuAnimationDefaultDuration;
@synthesize menuAnimationMaxDuration;
@synthesize shadow;
@synthesize elastic;


#pragma mark -
Expand Down Expand Up @@ -85,6 +86,7 @@ - (void)setDefaultSettings {
self.menuAnimationMaxDuration = 0.4f;
self.panMode = MFSideMenuPanModeDefault;
self.viewHasAppeared = NO;
self.elastic = NO;
}

- (void)setupMenuContainerView {
Expand Down Expand Up @@ -381,7 +383,7 @@ - (void)sendStateEventNotification:(MFSideMenuStateEvent)event {
#pragma mark -
#pragma mark - Side Menu Positioning

- (void) setLeftSideMenuFrameToClosedPosition {
- (void)setLeftSideMenuFrameToClosedPosition {
if(!self.leftMenuViewController) return;
CGRect leftFrame = [self.leftMenuViewController view].frame;
leftFrame.size.width = self.leftMenuWidth;
Expand All @@ -391,7 +393,7 @@ - (void) setLeftSideMenuFrameToClosedPosition {
[self.leftMenuViewController view].autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight;
}

- (void) setRightSideMenuFrameToClosedPosition {
- (void)setRightSideMenuFrameToClosedPosition {
if(!self.rightMenuViewController) return;
CGRect rightFrame = [self.rightMenuViewController view].frame;
rightFrame.size.width = self.rightMenuWidth;
Expand All @@ -406,7 +408,12 @@ - (void)alignLeftMenuControllerWithCenterViewController {
CGRect leftMenuFrame = [self.leftMenuViewController view].frame;
leftMenuFrame.size.width = _leftMenuWidth;

// prevent the slide from left animation from going past the menuWidth
CGFloat xOffset = [self.centerViewController view].frame.origin.x;
if (xOffset > self.leftMenuWidth) {
return;
}

CGFloat xPositionDivider = (self.menuSlideAnimationEnabled) ? self.menuSlideAnimationFactor : 1.0;
leftMenuFrame.origin.x = xOffset / xPositionDivider - _leftMenuWidth / xPositionDivider;

Expand All @@ -417,11 +424,16 @@ - (void)alignRightMenuControllerWithCenterViewController {
CGRect rightMenuFrame = [self.rightMenuViewController view].frame;
rightMenuFrame.size.width = _rightMenuWidth;

// prevent the slide from right animation from going past the menuWidth
CGFloat xOffset = [self.centerViewController view].frame.origin.x;
if (xOffset < -1*self.rightMenuWidth) {
return;
}

CGFloat xPositionDivider = (self.menuSlideAnimationEnabled) ? self.menuSlideAnimationFactor : 1.0;
rightMenuFrame.origin.x = self.menuContainerView.frame.size.width - _rightMenuWidth
+ xOffset / xPositionDivider
+ _rightMenuWidth / xPositionDivider;
+ xOffset / xPositionDivider
+ _rightMenuWidth / xPositionDivider;

[self.rightMenuViewController view].frame = rightMenuFrame;
}
Expand Down Expand Up @@ -483,11 +495,11 @@ - (void)setRightMenuWidth:(CGFloat)rightMenuWidth animated:(BOOL)animated {
#pragma mark -
#pragma mark - MFSideMenuPanMode

- (BOOL) centerViewControllerPanEnabled {
- (BOOL)centerViewControllerPanEnabled {
return ((self.panMode & MFSideMenuPanModeCenterViewController) == MFSideMenuPanModeCenterViewController);
}

- (BOOL) sideMenuPanEnabled {
- (BOOL)sideMenuPanEnabled {
return ((self.panMode & MFSideMenuPanModeSideMenu) == MFSideMenuPanModeSideMenu);
}

Expand All @@ -504,7 +516,7 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive
return [self centerViewControllerPanEnabled];

if([gestureRecognizer.view isEqual:self.menuContainerView])
return [self sideMenuPanEnabled];
return [self sideMenuPanEnabled];

// pan gesture is attached to a custom view
return YES;
Expand All @@ -528,7 +540,7 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer

// this method handles any pan event
// and sets the navigation controller's frame as needed
- (void) handlePan:(UIPanGestureRecognizer *)recognizer {
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
UIView *view = [self.centerViewController view];

if(recognizer.state == UIGestureRecognizerStateBegan) {
Expand All @@ -553,10 +565,10 @@ - (void) handlePan:(UIPanGestureRecognizer *)recognizer {
}
}

if((self.menuState == MFSideMenuStateRightMenuOpen && self.panDirection == MFSideMenuPanDirectionLeft)
|| (self.menuState == MFSideMenuStateLeftMenuOpen && self.panDirection == MFSideMenuPanDirectionRight)) {
if (!self.elastic &&
(self.menuState == MFSideMenuStateLeftMenuOpen && self.panDirection == MFSideMenuPanDirectionRight) &&
(self.menuState == MFSideMenuStateRightMenuOpen && self.panDirection == MFSideMenuPanDirectionLeft)) {
self.panDirection = MFSideMenuPanDirectionNone;
return;
}

if(self.panDirection == MFSideMenuPanDirectionLeft) {
Expand All @@ -566,7 +578,7 @@ - (void) handlePan:(UIPanGestureRecognizer *)recognizer {
}
}

- (void) handleRightPan:(UIPanGestureRecognizer *)recognizer {
- (void)handleRightPan:(UIPanGestureRecognizer *)recognizer {
if(!self.leftMenuViewController && self.menuState == MFSideMenuStateClosed) return;

UIView *view = [self.centerViewController view];
Expand All @@ -576,8 +588,12 @@ - (void) handleRightPan:(UIPanGestureRecognizer *)recognizer {
translatedPoint = CGPointMake(adjustedOrigin.x + translatedPoint.x,
adjustedOrigin.y + translatedPoint.y);

translatedPoint.x = MAX(translatedPoint.x, -1*self.rightMenuWidth);
translatedPoint.x = MIN(translatedPoint.x, self.leftMenuWidth);
// Allow user to pan past the edge if elastic is YES
if (!self.elastic) {
translatedPoint.x = MAX(translatedPoint.x, -1*self.rightMenuWidth);
translatedPoint.x = MIN(translatedPoint.x, self.leftMenuWidth);
}

if(self.menuState == MFSideMenuStateRightMenuOpen) {
// menu is already open, the most the user can do is close it in this gesture
translatedPoint.x = MIN(translatedPoint.x, 0);
Expand All @@ -591,7 +607,7 @@ - (void) handleRightPan:(UIPanGestureRecognizer *)recognizer {
CGFloat finalX = translatedPoint.x + (.35*velocity.x);
CGFloat viewWidth = view.frame.size.width;

if(self.menuState == MFSideMenuStateClosed) {
if (self.menuState == MFSideMenuStateClosed) {
BOOL showMenu = (finalX > viewWidth/2) || (finalX > self.leftMenuWidth/2);
if(showMenu) {
self.panGestureVelocity = velocity.x;
Expand All @@ -603,9 +619,17 @@ - (void) handleRightPan:(UIPanGestureRecognizer *)recognizer {
} else {
BOOL hideMenu = (finalX > adjustedOrigin.x);
if(hideMenu) {
if (self.elastic && self.menuState == MFSideMenuStateLeftMenuOpen) {
[self setMenuState:MFSideMenuStateLeftMenuOpen];
return;
}
self.panGestureVelocity = velocity.x;
[self setMenuState:MFSideMenuStateClosed];
} else {
if (self.elastic && self.menuState == MFSideMenuStateLeftMenuOpen) {
[self setMenuState:MFSideMenuStateClosed];
return;
}
self.panGestureVelocity = 0;
[self setCenterViewControllerOffset:adjustedOrigin.x animated:YES completion:nil];
}
Expand All @@ -617,7 +641,7 @@ - (void) handleRightPan:(UIPanGestureRecognizer *)recognizer {
}
}

- (void) handleLeftPan:(UIPanGestureRecognizer *)recognizer {
- (void)handleLeftPan:(UIPanGestureRecognizer *)recognizer {
if(!self.rightMenuViewController && self.menuState == MFSideMenuStateClosed) return;

UIView *view = [self.centerViewController view];
Expand All @@ -627,8 +651,12 @@ - (void) handleLeftPan:(UIPanGestureRecognizer *)recognizer {
translatedPoint = CGPointMake(adjustedOrigin.x + translatedPoint.x,
adjustedOrigin.y + translatedPoint.y);

translatedPoint.x = MAX(translatedPoint.x, -1*self.rightMenuWidth);
translatedPoint.x = MIN(translatedPoint.x, self.leftMenuWidth);
// allow user to pan past the edge if elastic is enabled
if (!self.elastic) {
translatedPoint.x = MAX(translatedPoint.x, -1*self.rightMenuWidth);
translatedPoint.x = MIN(translatedPoint.x, self.leftMenuWidth);
}

if(self.menuState == MFSideMenuStateLeftMenuOpen) {
// don't let the pan go less than 0 if the menu is already open
translatedPoint.x = MAX(translatedPoint.x, 0);
Expand All @@ -637,8 +665,6 @@ - (void) handleLeftPan:(UIPanGestureRecognizer *)recognizer {
translatedPoint.x = MIN(translatedPoint.x, 0);
}

[self setCenterViewControllerOffset:translatedPoint.x];

if(recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:view];
CGFloat finalX = translatedPoint.x + (.35*velocity.x);
Expand All @@ -656,9 +682,17 @@ - (void) handleLeftPan:(UIPanGestureRecognizer *)recognizer {
} else {
BOOL hideMenu = (finalX < adjustedOrigin.x);
if(hideMenu) {
if (self.elastic && self.menuState == MFSideMenuStateRightMenuOpen) {
[self setMenuState:MFSideMenuStateRightMenuOpen];
return;
}
self.panGestureVelocity = velocity.x;
[self setMenuState:MFSideMenuStateClosed];
} else {
if (self.elastic && self.menuState == MFSideMenuStateRightMenuOpen) {
[self setMenuState:MFSideMenuStateClosed];
return;
}
self.panGestureVelocity = 0;
[self setCenterViewControllerOffset:adjustedOrigin.x animated:YES completion:nil];
}
Expand Down Expand Up @@ -743,6 +777,7 @@ - (CGFloat)animationDurationFromStartPosition:(CGFloat)startPosition toEndPositi
if(ABS(self.panGestureVelocity) > 1.0) {
// try to continue the animation at the speed the user was swiping
duration = animationPositionDelta / ABS(self.panGestureVelocity);
self.panGestureVelocity = 0.0;
} else {
// no swipe was used, user tapped the bar button item
// TODO: full animation duration hard to calculate with two menu widths
Expand Down
8 changes: 8 additions & 0 deletions README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ You can add panning to any view like so:
[panView addGestureRecognizer:[self.menuContainerViewController panGestureRecognizer];
```

###Elasticity

You can enable elasticity which allows the user to pan beyond the edges of the screen for a more natural feel. By default, this feature is disabled.

```objective-c
self.menuContainerViewController.elastic = YES;
```

###Listening for Menu Events

You can listen for menu state event changes (i.e. menu will open, menu did open, etc.). See MFSideMenuContainerViewController.h for the different types of events.
Expand Down