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

Fading menues and optional protocol for overriding gesture #132

Open
wants to merge 3 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
15 changes: 15 additions & 0 deletions PKRevealController/Controller/PKRevealController.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#import <UIKit/UIKit.h>
#import "UIViewController+PKRevealController.h"


@protocol PKPanGestureControlProtocol <NSObject>
-(BOOL)allowPanGestureForRecognizer:(UIGestureRecognizer *)gestureRecognizer withTouch:(UITouch *)touch;
@end

typedef NS_ENUM(NSUInteger, PKRevealControllerState)
{
PKRevealControllerFocusesLeftViewController,
Expand All @@ -39,6 +44,15 @@ typedef NS_OPTIONS(NSUInteger, PKRevealControllerType)
* List of option keys that can be passed in the options dictionary.
*/

/*
* Determines whether the left and right side menues should fade in/out when being slided
*
* @default NO
* @value NSNumber containing BOOL
*/
extern NSString * const PKRevealControllerFadeMenuesWhileSlidingKey;


/*
* Animation duration for automatic front view movement.
*
Expand Down Expand Up @@ -127,6 +141,7 @@ typedef void(^PKDefaultErrorHandler)(NSError *error);
@property (nonatomic, assign, readwrite) PKRevealControllerAnimationType animationType;
@property (nonatomic, assign, readwrite) CGFloat quickSwipeVelocity;
@property (nonatomic, assign, readwrite) BOOL allowsOverdraw;
@property (nonatomic, assign, readwrite) BOOL fadeMenuesWhileSliding;
@property (nonatomic, assign, readwrite) BOOL disablesFrontViewInteraction;
@property (nonatomic, assign, readwrite) BOOL recognizesPanningOnFrontView;
@property (nonatomic, assign, readwrite) BOOL recognizesResetTapOnFrontView;
Expand Down
83 changes: 83 additions & 0 deletions PKRevealController/Controller/PKRevealController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define DEFAULT_ALLOWS_OVERDRAW_VALUE YES
#define DEFAULT_ANIMATION_TYPE_VALUE PKRevealControllerAnimationTypeStatic
#define DEFAULT_QUICK_SWIPE_TOGGLE_VELOCITY_VALUE 800.0f
#define DEFAULT_FADE_MENUES_WHILE_SLIDING NO
#define DEFAULT_DISABLES_FRONT_VIEW_INTERACTION_VALUE YES
#define DEFAULT_RECOGNIZES_PAN_ON_FRONT_VIEW_VALUE YES
#define DEFAULT_RECOGNIZES_RESET_TAP_ON_FRONT_VIEW_VALUE YES
Expand Down Expand Up @@ -52,6 +53,7 @@ @interface PKRevealController ()

@implementation PKRevealController

NSString * const PKRevealControllerFadeMenuesWhileSlidingKey = @"PKRevealControllerFadeMenuesWhileSlidingKey";
NSString * const PKRevealControllerAnimationDurationKey = @"PKRevealControllerAnimationDurationKey";
NSString * const PKRevealControllerAnimationCurveKey = @"PKRevealControllerAnimationCurveKey";
NSString * const PKRevealControllerAnimationTypeKey = @"PKRevealControllerAnimationTypeKey";
Expand Down Expand Up @@ -704,6 +706,29 @@ - (CGFloat)quickSwipeVelocity

#pragma mark -

- (BOOL)fadeMenuesWhileSliding
{
NSNumber *number = [self.controllerOptions objectForKey:PKRevealControllerFadeMenuesWhileSlidingKey];

if (number == nil)
{
[self setFadeMenuesWhileSliding:DEFAULT_FADE_MENUES_WHILE_SLIDING];
return [self fadeMenuesWhileSliding];
}
else
{
return [number boolValue];
}
}

- (void)setFadeMenuesWhileSliding:(BOOL)fadeMenuesWhileSliding
{
[self.controllerOptions setObject:[NSNumber numberWithBool:fadeMenuesWhileSliding]
forKey:PKRevealControllerFadeMenuesWhileSlidingKey];
}



- (BOOL)disablesFrontViewInteraction
{
NSNumber *number = [self.controllerOptions objectForKey:PKRevealControllerDisablesFrontViewInteractionKey];
Expand Down Expand Up @@ -817,6 +842,23 @@ - (void)handleGestureBeganWithRecognizer:(UIPanGestureRecognizer *)recognizer
- (void)handleGestureChangedWithRecognizer:(UIPanGestureRecognizer *)recognizer
{
CGPoint currentTouchLocation = [recognizer locationInView:self.view];


if (self.fadeMenuesWhileSliding)
{
if ([self isLeftViewVisible])
{
float newAlpha = currentTouchLocation.x/self.leftViewWidthRange.length;
[self.leftViewContainer setAlpha:newAlpha];
}
else if ([self isRightViewVisible])
{
float newAlpha = currentTouchLocation.x/self.rightViewWidthRange.length;
[self.rightViewContainer setAlpha:newAlpha];
}
}


CGFloat delta = currentTouchLocation.x - self.previousTouchLocation.x;
self.previousTouchLocation = currentTouchLocation;

Expand Down Expand Up @@ -861,6 +903,31 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
BOOL allowGesture = YES;

UIViewController* visibleVC = self.frontViewController;

if ([self.frontViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController* navController = (UINavigationController*)self.frontViewController;
visibleVC = navController.visibleViewController;
}

if ([visibleVC conformsToProtocol:@protocol(PKPanGestureControlProtocol)] &&
!([self isLeftViewVisible] || [self isRightViewVisible]) )
{
UIViewController<PKPanGestureControlProtocol>* vcProtocol =
(UIViewController<PKPanGestureControlProtocol> *)visibleVC;

allowGesture = [vcProtocol allowPanGestureForRecognizer:gestureRecognizer withTouch:touch];
}

return allowGesture;
}


#pragma mark - Translation

- (void)translateViewsBy:(CGFloat)delta animationType:(PKRevealControllerAnimationType)animationType
Expand Down Expand Up @@ -959,6 +1026,14 @@ - (void)showLeftViewControllerAnimated:(BOOL)animated
[weakSelf removeRightViewControllerFromHierarchy];
[weakSelf addLeftViewControllerToHierarchy];

if (weakSelf.fadeMenuesWhileSliding)
{
[UIView animateWithDuration:DEFAULT_ANIMATION_DURATION_VALUE
animations:^{
[weakSelf.leftViewContainer setAlpha:1.0];
}];
}

[weakSelf setFrontViewFrame:[weakSelf frontViewFrameForVisibleLeftView]
animated:animated
completion:^(BOOL finished)
Expand Down Expand Up @@ -999,6 +1074,14 @@ - (void)showRightViewControllerAnimated:(BOOL)animated
[weakSelf removeLeftViewControllerFromHierarchy];
[weakSelf addRightViewControllerToHierarchy];

if (weakSelf.fadeMenuesWhileSliding)
{
[UIView animateWithDuration:DEFAULT_ANIMATION_DURATION_VALUE
animations:^{
[weakSelf.rightViewContainer setAlpha:1.0];
}];
}

[weakSelf setFrontViewFrame:[weakSelf frontViewFrameForVisibleRightView]
animated:animated
completion:^(BOOL finished)
Expand Down