Skip to content
This repository has been archived by the owner on Sep 11, 2022. It is now read-only.

Dynamic enable/disable swiping to left/right viewControllers. #143

Open
wants to merge 1 commit 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 PKRevealController/Controller/PKRevealController.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ typedef void(^PKDefaultErrorHandler)(NSError *error);
@property (nonatomic, assign, readwrite) BOOL allowsOverdraw;
@property (nonatomic, assign, readwrite) BOOL disablesFrontViewInteraction;
@property (nonatomic, assign, readwrite) BOOL recognizesPanningOnFrontView;
@property (nonatomic, assign, readwrite) BOOL recognizesPanningLeftOnFrontView;
@property (nonatomic, assign, readwrite) BOOL recognizesPanningRightOnFrontView;
@property (nonatomic, assign, readwrite) BOOL recognizesResetTapOnFrontView;

#pragma mark - Methods
Expand Down
137 changes: 82 additions & 55 deletions PKRevealController/Controller/PKRevealController.m
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ - (void)setup
self.state = PKRevealControllerFocusesFrontViewController;
self.leftViewWidthRange = DEFAULT_LEFT_VIEW_WIDTH_RANGE;
self.rightViewWidthRange = DEFAULT_RIGHT_VIEW_WIDTH_RANGE;
self.recognizesPanningLeftOnFrontView = YES;
self.recognizesPanningRightOnFrontView = YES;

self.view.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
}
Expand Down Expand Up @@ -782,6 +784,17 @@ - (void)didRecognizeTapWithGestureRecognizer:(UITapGestureRecognizer *)recognize

- (void)didRecognizePanWithGestureRecognizer:(UIPanGestureRecognizer *)recognizer
{
//
// If a frontViewController is rootViewController of a UINavigationController
// and it has pushed another viewController, we may wish to deny that viewController
// swipe access to the leftViewController while allowing swipe to the rightViewController
// or vice versa.
//

CGPoint velocity = [recognizer velocityInView:self.view];
if ((velocity.x > 0) && ([self isRightViewVisible] == NO) && (self.recognizesPanningLeftOnFrontView == NO)) return;
if ((velocity.x < 0) && ([self isLeftViewVisible] == NO) && (self.recognizesPanningRightOnFrontView == NO)) return;

switch (recognizer.state)
{
case UIGestureRecognizerStateBegan:
Expand All @@ -797,7 +810,7 @@ - (void)didRecognizePanWithGestureRecognizer:(UIPanGestureRecognizer *)recognize
case UIGestureRecognizerStateFailed:
[self handleGestureEndedWithRecognizer:recognizer];
break;

default:
{
self.revealPanGestureRecognizer.enabled = YES;
Expand Down Expand Up @@ -856,7 +869,7 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
else if (gestureRecognizer == self.revealResetTapGestureRecognizer)
{
return ([self isLeftViewVisible] || [self isRightViewVisible]);
}
}

return YES;
}
Expand All @@ -877,11 +890,25 @@ - (void)translateViewsBy:(CGFloat)delta animationType:(PKRevealControllerAnimati
BOOL negativeTranslationDoesNotExceedMinWidth = (translation > CGRectGetMinX(frameForFrontViewCenter)-[self rightViewMinWidth]);
BOOL negativeTranslationDoesNotExceedMaxWidth = (translation > CGRectGetMinX(frameForFrontViewCenter)-[self rightViewMaxWidthRespectingOverdraw:YES]);

BOOL isLegalNormalTranslation = ([self hasLeftViewController] && isPositiveTranslation && positiveTranslationDoesNotExceedMinWidth)
|| ([self hasRightViewController] && isNegativeTranslation && negativeTranslationDoesNotExceedMinWidth);
/*
BOOL isLegalNormalTranslation = ([self hasLeftViewController] && isPositiveTranslation && positiveTranslationDoesNotExceedMinWidth)
|| ([self hasRightViewController] && isNegativeTranslation && negativeTranslationDoesNotExceedMinWidth);

BOOL isLegalOverdrawTranslation = ([self hasLeftViewController] && isPositiveTranslation && positiveTranslationDoesNotExceedMaxWidth)
|| ([self hasRightViewController] && isNegativeTranslation && negativeTranslationDoesNotExceedMaxWidth);
*/

//
// We are in the middle of the animation so we'll need to protect the leftViewController or rightViewController from
// showing a hidden viewController on 'overswipe' provided the user has set either the recognizesPanningLeftOnFrontView
// or recognizesPanningRightOnFrontView to NO.
//

BOOL isLegalNormalTranslation = ([self hasLeftViewController] && isPositiveTranslation && positiveTranslationDoesNotExceedMinWidth && (self.recognizesPanningLeftOnFrontView))
|| ([self hasRightViewController] && isNegativeTranslation && negativeTranslationDoesNotExceedMinWidth && (self.recognizesPanningRightOnFrontView));

BOOL isLegalOverdrawTranslation = ([self hasLeftViewController] && isPositiveTranslation && positiveTranslationDoesNotExceedMaxWidth)
|| ([self hasRightViewController] && isNegativeTranslation && negativeTranslationDoesNotExceedMaxWidth);
BOOL isLegalOverdrawTranslation = ([self hasLeftViewController] && isPositiveTranslation && positiveTranslationDoesNotExceedMaxWidth && (self.recognizesPanningLeftOnFrontView))
|| ([self hasRightViewController] && isNegativeTranslation && negativeTranslationDoesNotExceedMaxWidth && (self.recognizesPanningRightOnFrontView));

if (isLegalNormalTranslation || isLegalOverdrawTranslation)
{
Expand Down Expand Up @@ -978,9 +1005,9 @@ - (void)showLeftViewControllerAnimated:(BOOL)animated
{
[self showFrontViewControllerAnimated:animated
completion:^(BOOL finished)
{
showLeftViewBlock(finished);
}];
{
showLeftViewBlock(finished);
}];
}
else
{
Expand All @@ -1002,24 +1029,24 @@ - (void)showRightViewControllerAnimated:(BOOL)animated
[weakSelf setFrontViewFrame:[weakSelf frontViewFrameForVisibleRightView]
animated:animated
completion:^(BOOL finished)
{
if (weakSelf.disablesFrontViewInteraction)
{
[weakSelf.frontViewContainer disableUserInteractionForContainedView];
}
weakSelf.state = PKRevealControllerFocusesRightViewController;
[weakSelf updateResetTapGestureRecognizer];
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
{
if (weakSelf.disablesFrontViewInteraction)
{
[weakSelf.frontViewContainer disableUserInteractionForContainedView];
}
weakSelf.state = PKRevealControllerFocusesRightViewController;
[weakSelf updateResetTapGestureRecognizer];
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
};

if ([self isLeftViewVisible])
{
[self showFrontViewControllerAnimated:animated
completion:^(BOOL finished)
{
showRightViewBlock(finished);
}];
{
showRightViewBlock(finished);
}];
}
else
{
Expand Down Expand Up @@ -1057,10 +1084,10 @@ - (void)enterPresentationModeForLeftViewControllerAnimated:(BOOL)animated
[self setFrontViewFrame:[self frontViewFrameForLeftViewPresentationMode]
animated:animated
completion:^(BOOL finished)
{
weakSelf.state = PKRevealControllerFocusesLeftViewControllerInPresentationMode;
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
{
weakSelf.state = PKRevealControllerFocusesLeftViewControllerInPresentationMode;
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
}

- (void)enterPresentationModeForRightViewControllerAnimated:(BOOL)animated
Expand All @@ -1071,10 +1098,10 @@ - (void)enterPresentationModeForRightViewControllerAnimated:(BOOL)animated
[self setFrontViewFrame:[self frontViewFrameForRightViewPresentationMode]
animated:animated
completion:^(BOOL finished)
{
weakSelf.state = PKRevealControllerFocusesRightViewControllerInPresentationMode;
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
{
weakSelf.state = PKRevealControllerFocusesRightViewControllerInPresentationMode;
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
}

- (void)resignPresentationModeForLeftViewControllerEntirely:(BOOL)entirely
Expand All @@ -1100,10 +1127,10 @@ - (void)resignPresentationModeForLeftViewControllerEntirely:(BOOL)entirely
[self setFrontViewFrame:frame
animated:animated
completion:^(BOOL finished)
{
weakSelf.state = state;
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
{
weakSelf.state = state;
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
}

- (void)resignPresentationModeForRightViewControllerEntirely:(BOOL)entirely
Expand All @@ -1127,10 +1154,10 @@ - (void)resignPresentationModeForRightViewControllerEntirely:(BOOL)entirely
}

[self setFrontViewFrame:frame animated:animated completion:^(BOOL finished)
{
weakSelf.state = state;
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
{
weakSelf.state = state;
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
}

#pragma mark -
Expand Down Expand Up @@ -1159,13 +1186,13 @@ - (void)setFrontViewFrameLinearly:(CGRect)frame
completion:(PKDefaultCompletionHandler)completion
{
[UIView animateWithDuration:duration delay:0.0f options:options animations:^
{
self.frontViewContainer.frame = frame;
}
completion:^(BOOL finished)
{
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
{
self.frontViewContainer.frame = frame;
}
completion:^(BOOL finished)
{
safelyExecuteCompletionBlockOnMainThread(completion, finished);
}];
}

#pragma mark - Helpers (Gestures)
Expand Down Expand Up @@ -1388,18 +1415,18 @@ - (BOOL)shouldAutorotate
if ([self hasLeftViewController] && [self hasRightViewController])
{
return [self.frontViewController shouldAutorotate]
&& [self.leftViewController shouldAutorotate]
&& [self.rightViewController shouldAutorotate];
&& [self.leftViewController shouldAutorotate]
&& [self.rightViewController shouldAutorotate];
}
else if ([self hasLeftViewController])
{
return [self.frontViewController shouldAutorotate]
&& [self.leftViewController shouldAutorotate];
&& [self.leftViewController shouldAutorotate];
}
else if ([self hasRightViewController])
{
return [self.frontViewController shouldAutorotate]
&& [self.rightViewController shouldAutorotate];
&& [self.rightViewController shouldAutorotate];
}
else
{
Expand All @@ -1412,18 +1439,18 @@ - (NSUInteger)supportedInterfaceOrientations
if ([self hasLeftViewController] && [self hasRightViewController])
{
return self.frontViewController.supportedInterfaceOrientations
& self.leftViewController.supportedInterfaceOrientations
& self.rightViewController.supportedInterfaceOrientations;
& self.leftViewController.supportedInterfaceOrientations
& self.rightViewController.supportedInterfaceOrientations;
}
else if ([self hasLeftViewController])
{
return self.frontViewController.supportedInterfaceOrientations
& self.leftViewController.supportedInterfaceOrientations;
& self.leftViewController.supportedInterfaceOrientations;
}
else if ([self hasRightViewController])
{
return self.frontViewController.supportedInterfaceOrientations
& self.rightViewController.supportedInterfaceOrientations;
& self.rightViewController.supportedInterfaceOrientations;
}
else
{
Expand All @@ -1434,8 +1461,8 @@ - (NSUInteger)supportedInterfaceOrientations
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return [self.frontViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]
&& [self.leftViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]
&& [self.rightViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
&& [self.leftViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]
&& [self.rightViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
Expand All @@ -1455,7 +1482,7 @@ - (void)dealloc
[self.leftViewController removeFromParentViewController];
[self.leftViewController.view removeFromSuperview];
self.leftViewContainer = nil;

[self.rightViewController removeFromParentViewController];
[self.rightViewController.view removeFromSuperview];
self.rightViewContainer = nil;
Expand Down