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

Adds support for setting the shadow radius and opacity. #270

Closed
wants to merge 5 commits into from
Closed
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
29 changes: 29 additions & 0 deletions MMDrawerController/MMDrawerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ typedef NS_ENUM(NSInteger, MMDrawerOpenCenterInteractionMode) {

@class MMDrawerController;
typedef void (^MMDrawerControllerDrawerVisualStateBlock)(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible);
typedef void (^MMDrawerControllerOpenDrawerCompletionBlock)(MMDrawerController * drawerController, MMDrawerSide drawerSide);

@interface MMDrawerController : UIViewController

Expand Down Expand Up @@ -165,6 +166,13 @@ typedef void (^MMDrawerControllerDrawerVisualStateBlock)(MMDrawerController * dr
*/
@property (nonatomic, assign) BOOL shouldStretchDrawer;

/**
The maximum overshoot to use when stretching the drawer.

If not set then the overshoot is calculated based on the width of the center and drawer views.
*/
@property (nonatomic, assign) CGFloat maxOvershootForStretchingDrawer;

/**
The current open side of the drawer.

Expand Down Expand Up @@ -200,6 +208,20 @@ typedef void (^MMDrawerControllerDrawerVisualStateBlock)(MMDrawerController * dr
*/
@property (nonatomic, assign) BOOL showsShadow;

/**
The value determining the shadow radius to use if a shadow should be drawn off of `centerViewController` when a drawer is open.

By default, this is set to 10.0
*/
@property (nonatomic, assign) CGFloat shadowRadius;

/**
The value determining the shadow opacity to use if a shadow should be drawn off of `centerViewController` when a drawer is open.

By default, this is set to 0.8
*/
@property (nonatomic, assign) CGFloat shadowOpacity;

/**
The flag determining if a custom background view should appear beneath the status bar, forcing the child content to be drawn lower than the status bar. This property is only available for > iOS 7.0 to take into account for the new behavior of the status bar.

Expand Down Expand Up @@ -410,4 +432,11 @@ typedef void (^MMDrawerControllerDrawerVisualStateBlock)(MMDrawerController * dr
*/
-(void)setGestureShouldRecognizeTouchBlock:(BOOL(^)(MMDrawerController * drawerController, UIGestureRecognizer * gesture, UITouch * touch))gestureShouldRecognizeTouchBlock;

/**
Sets a callback to be called when a drawer is fully opened.

@param openDrawerCompletionBlock A block object to be called that allows the implementer be notified when a drawer is fully opened.
*/
-(void)setOpenDrawerCompletionBlock:(void(^)(MMDrawerController * drawerController, MMDrawerSide drawerSide))openDrawerCompletionBlock;

@end
37 changes: 29 additions & 8 deletions MMDrawerController/MMDrawerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ @interface MMDrawerController () <UIGestureRecognizerDelegate>{
@property (nonatomic, copy) MMDrawerControllerDrawerVisualStateBlock drawerVisualState;
@property (nonatomic, copy) MMDrawerGestureShouldRecognizeTouchBlock gestureShouldRecognizeTouch;
@property (nonatomic, copy) MMDrawerGestureCompletionBlock gestureCompletion;
@property (nonatomic, copy) MMDrawerControllerOpenDrawerCompletionBlock openDrawerCompletion;
@property (nonatomic, assign, getter = isAnimatingDrawer) BOOL animatingDrawer;

@end
Expand Down Expand Up @@ -186,6 +187,8 @@ -(void)commonSetup{
[self setAnimationVelocity:MMDrawerDefaultAnimationVelocity];

[self setShowsShadow:YES];
[self setShadowRadius:MMDrawerDefaultShadowRadius];
[self setShadowOpacity:MMDrawerDefaultShadowOpacity];
[self setShouldStretchDrawer:YES];

[self setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
Expand Down Expand Up @@ -253,7 +256,8 @@ -(void)toggleDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated complet
}

-(void)closeDrawerAnimated:(BOOL)animated completion:(void (^)(BOOL finished))completion{
[self closeDrawerAnimated:animated velocity:self.animationVelocity animationOptions:UIViewAnimationOptionCurveEaseInOut completion:completion];
CGFloat animationVelocity = self.animationVelocity ?: (self.openSide == MMDrawerSideLeft ? self.maximumLeftDrawerWidth : self.maximumRightDrawerWidth) * 3.0;
[self closeDrawerAnimated:animated velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseInOut completion:completion];
}

-(void)closeDrawerAnimated:(BOOL)animated velocity:(CGFloat)velocity animationOptions:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion{
Expand Down Expand Up @@ -315,8 +319,9 @@ -(void)closeDrawerAnimated:(BOOL)animated velocity:(CGFloat)velocity animationOp

-(void)openDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated completion:(void (^)(BOOL finished))completion{
NSParameterAssert(drawerSide != MMDrawerSideNone);

[self openDrawerSide:drawerSide animated:animated velocity:self.animationVelocity animationOptions:UIViewAnimationOptionCurveEaseInOut completion:completion];

CGFloat animationVelocity = self.animationVelocity ?: (drawerSide == MMDrawerSideLeft ? self.maximumLeftDrawerWidth : self.maximumRightDrawerWidth) * 3.0;
[self openDrawerSide:drawerSide animated:animated velocity:animationVelocity animationOptions:UIViewAnimationOptionCurveEaseInOut completion:completion];
}

-(void)openDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated velocity:(CGFloat)velocity animationOptions:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion{
Expand Down Expand Up @@ -369,6 +374,9 @@ -(void)openDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated velocity:
if(completion){
completion(finished);
}
if (finished && self.openDrawerCompletion) {
self.openDrawerCompletion(self,drawerSide);
}
}];
}
}
Expand Down Expand Up @@ -649,6 +657,10 @@ -(void)bouncePreviewForDrawerSide:(MMDrawerSide)drawerSide distance:(CGFloat)dis
}
}

-(void)setOpenDrawerCompletionBlock:(void(^)(MMDrawerController * drawerController, MMDrawerSide drawerSide))openDrawerCompletionBlock{
[self setOpenDrawerCompletion:openDrawerCompletionBlock];
}

#pragma mark - Setting Drawer Visual State
-(void)setDrawerVisualStateBlock:(void (^)(MMDrawerController *, MMDrawerSide, CGFloat))drawerVisualStateBlock{
[self setDrawerVisualState:drawerVisualStateBlock];
Expand Down Expand Up @@ -1176,7 +1188,7 @@ -(CGFloat)roundedOriginXForDrawerConstriants:(CGFloat)originX{
if (originX < -self.maximumRightDrawerWidth) {
if (self.shouldStretchDrawer &&
self.rightDrawerViewController) {
CGFloat maxOvershoot = (CGRectGetWidth(self.centerContainerView.frame)-self.maximumRightDrawerWidth)*MMDrawerOvershootPercentage;
CGFloat maxOvershoot = self.maxOvershootForStretchingDrawer ?: (CGRectGetWidth(self.centerContainerView.frame)-self.maximumRightDrawerWidth)*MMDrawerOvershootPercentage;
return originXForDrawerOriginAndTargetOriginOffset(originX, -self.maximumRightDrawerWidth, maxOvershoot);
}
else{
Expand All @@ -1186,7 +1198,7 @@ -(CGFloat)roundedOriginXForDrawerConstriants:(CGFloat)originX{
else if(originX > self.maximumLeftDrawerWidth){
if (self.shouldStretchDrawer &&
self.leftDrawerViewController) {
CGFloat maxOvershoot = (CGRectGetWidth(self.centerContainerView.frame)-self.maximumLeftDrawerWidth)*MMDrawerOvershootPercentage;
CGFloat maxOvershoot = self.maxOvershootForStretchingDrawer ?: (CGRectGetWidth(self.centerContainerView.frame)-self.maximumLeftDrawerWidth)*MMDrawerOvershootPercentage;
return originXForDrawerOriginAndTargetOriginOffset(originX, self.maximumLeftDrawerWidth, maxOvershoot);
}
else{
Expand Down Expand Up @@ -1251,8 +1263,8 @@ -(void)updateShadowForCenterView{
UIView * centerView = self.centerContainerView;
if(self.showsShadow){
centerView.layer.masksToBounds = NO;
centerView.layer.shadowRadius = MMDrawerDefaultShadowRadius;
centerView.layer.shadowOpacity = MMDrawerDefaultShadowOpacity;
centerView.layer.shadowRadius = self.shadowRadius;
centerView.layer.shadowOpacity = self.shadowOpacity;

/** In the event this gets called a lot, we won't update the shadowPath
unless it needs to be updated (like during rotation) */
Expand All @@ -1275,7 +1287,16 @@ unless it needs to be updated (like during rotation) */
}

-(NSTimeInterval)animationDurationForAnimationDistance:(CGFloat)distance{
NSTimeInterval duration = MAX(distance/self.animationVelocity,MMDrawerMinimumAnimationDuration);
CGFloat animationVelocity = self.animationVelocity;
if (!animationVelocity){
if (self.openSide == MMDrawerSideNone){
animationVelocity = MMDrawerDefaultAnimationVelocity;
}
else{
animationVelocity = (self.openSide == MMDrawerSideLeft ? self.maximumLeftDrawerWidth : self.maximumRightDrawerWidth) * 3.0;
}
}
NSTimeInterval duration = MAX(distance/animationVelocity,MMDrawerMinimumAnimationDuration);
return duration;
}

Expand Down
4 changes: 3 additions & 1 deletion MMDrawerController/MMDrawerVisualState.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@
Creates a parallax experience that slides the side drawer view controller at a different rate than the center view controller during animation. For every parallaxFactor of points moved by the center view controller, the side drawer view controller will move 1 point. Passing in 1.0 is the equivalent of a applying a sliding animation, while passing in MAX_FLOAT is the equivalent of having no animation at all.

@param parallaxFactor The amount of parallax applied to the side drawer conroller. This value must be greater than 1.0. The closer the value is to 1.0, the faster the side drawer view controller will be parallaxing.

@param shouldStretchDrawer Determines whether or not to strectch the drawer when the percentVisible is greater than 1.0. It is YES by default if using the first method.

@return The visual state block.
*/
+(MMDrawerControllerDrawerVisualStateBlock)parallaxVisualStateBlockWithParallaxFactor:(CGFloat)parallaxFactor;
+(MMDrawerControllerDrawerVisualStateBlock)parallaxVisualStateBlockWithParallaxFactor:(CGFloat)parallaxFactor shouldStretchDrawer:(BOOL)shouldStretchDrawer;

@end
19 changes: 14 additions & 5 deletions MMDrawerController/MMDrawerVisualState.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ +(MMDrawerControllerDrawerVisualStateBlock)swingingDoorVisualStateBlock{
return visualStateBlock;
}

+(MMDrawerControllerDrawerVisualStateBlock)parallaxVisualStateBlockWithParallaxFactor:(CGFloat)parallaxFactor{
+(MMDrawerControllerDrawerVisualStateBlock)parallaxVisualStateBlockWithParallaxFactor:(CGFloat)parallaxFactor
{
return [self parallaxVisualStateBlockWithParallaxFactor:parallaxFactor shouldStretchDrawer:YES];
}

+(MMDrawerControllerDrawerVisualStateBlock)parallaxVisualStateBlockWithParallaxFactor:(CGFloat)parallaxFactor shouldStretchDrawer:(BOOL)shouldStretchDrawer{
MMDrawerControllerDrawerVisualStateBlock visualStateBlock =
^(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible){
NSParameterAssert(parallaxFactor >= 1.0);
Expand All @@ -126,8 +131,10 @@ +(MMDrawerControllerDrawerVisualStateBlock)parallaxVisualStateBlockWithParallaxF
transform = CATransform3DMakeTranslation((-distance)/parallaxFactor+(distance*percentVisible/parallaxFactor), 0.0, 0.0);
}
else{
transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
transform = CATransform3DTranslate(transform, drawerController.maximumLeftDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
if (shouldStretchDrawer){
transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
transform = CATransform3DTranslate(transform, drawerController.maximumLeftDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
}
}
}
else if(drawerSide == MMDrawerSideRight){
Expand All @@ -137,8 +144,10 @@ +(MMDrawerControllerDrawerVisualStateBlock)parallaxVisualStateBlockWithParallaxF
transform = CATransform3DMakeTranslation((distance)/parallaxFactor-(distance*percentVisible)/parallaxFactor, 0.0, 0.0);
}
else{
transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
transform = CATransform3DTranslate(transform, -drawerController.maximumRightDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
if (shouldStretchDrawer){
transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
transform = CATransform3DTranslate(transform, -drawerController.maximumRightDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
}
}
}

Expand Down