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

Support smoother completion animation for panning velocity #18

Merged
merged 2 commits into from
May 16, 2013
Merged
Changes from 1 commit
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
30 changes: 20 additions & 10 deletions MMDrawerController/MMDrawerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

CGFloat const MMDrawerBezelRange = 20.0f;

CGFloat const MMDrawerPanVelocityXAnimationThreshold = 100.0f;
CGFloat const MMDrawerPanVelocityXAnimationThreshold = 200.0f;

/** The amount of overshoot that is panned linearly. The remaining percentage nonlinearly asymptotes to the max percentage. */
CGFloat const MMDrawerOvershootLinearRangePercentage = 0.75f;
Expand Down Expand Up @@ -181,10 +181,14 @@ -(void)toggleDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated complet
}

-(void)closeDrawerAnimated:(BOOL)animated completion:(void (^)(BOOL))completion{
[self closeDrawerAnimated:animated velocity:self.animationVelocity animationOptions:UIViewAnimationOptionCurveEaseInOut completion:completion];
}

-(void)closeDrawerAnimated:(BOOL)animated velocity:(CGFloat)velocity animationOptions:(UIViewAnimationOptions)options completion:(void (^)(BOOL))completion{
CGRect newFrame = self.view.bounds;

CGFloat distance = ABS(CGRectGetMinX(self.centerContainerView.frame));
NSTimeInterval duration = [self animationDurationForAnimationDistance:distance];
NSTimeInterval duration = MAX(distance/ABS(velocity),MMDrawerMinimumAnimationDuration);

BOOL leftDrawerVisible = CGRectGetMinX(self.centerContainerView.frame) > 0;
BOOL rightDrawerVisible = CGRectGetMinX(self.centerContainerView.frame) < 0;
Expand Down Expand Up @@ -212,14 +216,14 @@ -(void)closeDrawerAnimated:(BOOL)animated completion:(void (^)(BOOL))completion{
[UIView
animateWithDuration:(animated?duration:0.0)
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
options:options
animations:^{
[self.centerContainerView setFrame:newFrame];
[self updateDrawerVisualStateForDrawerSide:visibleSide percentVisible:0.0];
}
completion:^(BOOL finished) {
[sideDrawerViewController endAppearanceTransition];
[self setOpenSide:MMDrawerSideNone];
[self setOpenSide:MMDrawerSideNone];
[self resetDrawerVisualStateForDrawerSide:visibleSide];

if(completion){
Expand All @@ -231,6 +235,12 @@ -(void)closeDrawerAnimated:(BOOL)animated completion:(void (^)(BOOL))completion{
-(void)openDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated completion:(void (^)(BOOL))completion{
NSParameterAssert(drawerSide != MMDrawerSideNone);

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

-(void)openDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated velocity:(CGFloat)velocity animationOptions:(UIViewAnimationOptions)options completion:(void (^)(BOOL))completion{
NSParameterAssert(drawerSide != MMDrawerSideNone);

UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:drawerSide];
CGRect visibleRect = CGRectIntersection(self.view.bounds,sideDrawerViewController.view.frame);
BOOL drawerFullyCovered = (CGRectContainsRect(self.centerContainerView.frame, visibleRect) ||
Expand All @@ -252,12 +262,12 @@ -(void)openDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated completio
}

CGFloat distance = ABS(CGRectGetMinX(oldFrame)-newFrame.origin.x);
NSTimeInterval duration = [self animationDurationForAnimationDistance:distance];
NSTimeInterval duration = MAX(distance/ABS(velocity),MMDrawerMinimumAnimationDuration);

[UIView
animateWithDuration:(animated?duration:0.0)
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
options:options
animations:^{
[self.centerContainerView setFrame:newFrame];
[self updateDrawerVisualStateForDrawerSide:drawerSide percentVisible:1.0];
Expand Down Expand Up @@ -792,10 +802,10 @@ -(void)finishAnimationForPanGestureWithXVelocity:(CGFloat)xVelocity completion:(
if(self.openSide == MMDrawerSideLeft) {
CGFloat midPoint = self.maximumLeftDrawerWidth / 2.0;
if(xVelocity > MMDrawerPanVelocityXAnimationThreshold){
[self openDrawerSide:MMDrawerSideLeft animated:YES completion:completion];
[self openDrawerSide:MMDrawerSideLeft animated:YES velocity:MAX(ABS(xVelocity),MMDrawerPanVelocityXAnimationThreshold*2) animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The velocity passed in here is the same in both places. Just break it out into a new animationVelocity variable and use that in both places.

}
else if(xVelocity < -MMDrawerPanVelocityXAnimationThreshold){
[self closeDrawerAnimated:YES completion:completion];
[self closeDrawerAnimated:YES velocity:MAX(ABS(xVelocity),MMDrawerPanVelocityXAnimationThreshold*2) animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
}
else if(currentOriginX < midPoint){
[self closeDrawerAnimated:YES completion:completion];
Expand All @@ -808,10 +818,10 @@ -(void)finishAnimationForPanGestureWithXVelocity:(CGFloat)xVelocity completion:(
currentOriginX = CGRectGetMaxX(self.centerContainerView.frame);
CGFloat midPoint = (CGRectGetWidth(self.view.bounds)-self.maximumRightDrawerWidth) + (self.maximumRightDrawerWidth / 2.0);
if(xVelocity > MMDrawerPanVelocityXAnimationThreshold){
[self closeDrawerAnimated:YES completion:completion];
[self closeDrawerAnimated:YES velocity:MAX(ABS(xVelocity),MMDrawerPanVelocityXAnimationThreshold*2) animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this calculated velocity here

}
else if (xVelocity < -MMDrawerPanVelocityXAnimationThreshold){
[self openDrawerSide:MMDrawerSideRight animated:YES completion:completion];
[self openDrawerSide:MMDrawerSideRight animated:YES velocity:MAX(ABS(xVelocity),MMDrawerPanVelocityXAnimationThreshold*2) animationOptions:UIViewAnimationOptionCurveEaseOut completion:completion];
}
else if(currentOriginX > midPoint){
[self closeDrawerAnimated:YES completion:completion];
Expand Down