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

a bug when using closeDrawerAnimated:velocity:animationOptions:completion: #71

Merged
merged 2 commits into from
Aug 13, 2013
Merged
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
182 changes: 99 additions & 83 deletions MMDrawerController/MMDrawerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ @interface MMDrawerController () <UIGestureRecognizerDelegate>{
@property (nonatomic, copy) MMDrawerControllerDrawerVisualStateBlock drawerVisualState;
@property (nonatomic, copy) MMDrawerGestureShouldRecognizeTouchBlock gestureShouldRecognizeTouch;
@property (nonatomic, copy) MMDrawerGestureCompletionBlock gestureCompletion;
@property (nonatomic, assign, getter = isAnimatingDrawer) BOOL animatingDrawer;

@end

Expand Down Expand Up @@ -193,109 +194,124 @@ -(void)closeDrawerAnimated:(BOOL)animated completion:(void (^)(BOOL))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 = MAX(distance/ABS(velocity),MMDrawerMinimumAnimationDuration);

BOOL leftDrawerVisible = CGRectGetMinX(self.centerContainerView.frame) > 0;
BOOL rightDrawerVisible = CGRectGetMinX(self.centerContainerView.frame) < 0;

MMDrawerSide visibleSide = MMDrawerSideNone;
CGFloat percentVisble = 0.0;

if(leftDrawerVisible){
CGFloat visibleDrawerPoints = CGRectGetMinX(self.centerContainerView.frame);
percentVisble = MAX(0.0,visibleDrawerPoints/self.maximumLeftDrawerWidth);
visibleSide = MMDrawerSideLeft;
}
else if(rightDrawerVisible){
CGFloat visibleDrawerPoints = CGRectGetWidth(self.centerContainerView.frame)-CGRectGetMaxX(self.centerContainerView.frame);
percentVisble = MAX(0.0,visibleDrawerPoints/self.maximumRightDrawerWidth);
visibleSide = MMDrawerSideRight;
}

UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:visibleSide];

[self updateDrawerVisualStateForDrawerSide:visibleSide percentVisible:percentVisble];

[sideDrawerViewController beginAppearanceTransition:NO animated:animated];

[UIView
animateWithDuration:(animated?duration:0.0)
delay:0.0
options:options
animations:^{
[self.centerContainerView setFrame:newFrame];
[self updateDrawerVisualStateForDrawerSide:visibleSide percentVisible:0.0];
}
completion:^(BOOL finished) {
[sideDrawerViewController endAppearanceTransition];
[self setOpenSide:MMDrawerSideNone];
[self resetDrawerVisualStateForDrawerSide:visibleSide];

if(completion){
completion(finished);
}
}];
}

-(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) ||
CGRectIsNull(visibleRect));
if(drawerFullyCovered){
[self prepareToPresentDrawer:drawerSide animated:animated];
if (self.isAnimatingDrawer) {
if(completion){
completion(NO);
}
}

if(sideDrawerViewController){
CGRect newFrame;
CGRect oldFrame = self.centerContainerView.frame;
if(drawerSide == MMDrawerSideLeft){
newFrame = self.centerContainerView.frame;
newFrame.origin.x = self.maximumLeftDrawerWidth;
else {
self.animatingDrawer = animated;
CGRect newFrame = self.view.bounds;

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

BOOL leftDrawerVisible = CGRectGetMinX(self.centerContainerView.frame) > 0;
BOOL rightDrawerVisible = CGRectGetMinX(self.centerContainerView.frame) < 0;

MMDrawerSide visibleSide = MMDrawerSideNone;
CGFloat percentVisble = 0.0;

if(leftDrawerVisible){
CGFloat visibleDrawerPoints = CGRectGetMinX(self.centerContainerView.frame);
percentVisble = MAX(0.0,visibleDrawerPoints/self.maximumLeftDrawerWidth);
visibleSide = MMDrawerSideLeft;
}
else {
newFrame = self.centerContainerView.frame;
newFrame.origin.x = 0-self.maximumRightDrawerWidth;
else if(rightDrawerVisible){
CGFloat visibleDrawerPoints = CGRectGetWidth(self.centerContainerView.frame)-CGRectGetMaxX(self.centerContainerView.frame);
percentVisble = MAX(0.0,visibleDrawerPoints/self.maximumRightDrawerWidth);
visibleSide = MMDrawerSideRight;
}

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

[self updateDrawerVisualStateForDrawerSide:visibleSide percentVisible:percentVisble];

[sideDrawerViewController beginAppearanceTransition:NO animated:animated];

[UIView
animateWithDuration:(animated?duration:0.0)
delay:0.0
options:options
animations:^{
[self.centerContainerView setFrame:newFrame];
[self updateDrawerVisualStateForDrawerSide:drawerSide percentVisible:1.0];
[self updateDrawerVisualStateForDrawerSide:visibleSide percentVisible:0.0];
}
completion:^(BOOL finished) {
//End the appearance transition if it already wasn't open.
if(drawerSide != self.openSide){
[sideDrawerViewController endAppearanceTransition];
}
[self setOpenSide:drawerSide];

[self resetDrawerVisualStateForDrawerSide:drawerSide];

[sideDrawerViewController endAppearanceTransition];
[self setOpenSide:MMDrawerSideNone];
[self resetDrawerVisualStateForDrawerSide:visibleSide];
[self setAnimatingDrawer:NO];
if(completion){
completion(finished);
}
}];
}
}

-(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);
if(self.isAnimatingDrawer){
if(completion){
completion(NO);
}
}
else {
UIViewController * sideDrawerViewController = [self sideDrawerViewControllerForSide:drawerSide];
CGRect visibleRect = CGRectIntersection(self.view.bounds,sideDrawerViewController.view.frame);
BOOL drawerFullyCovered = (CGRectContainsRect(self.centerContainerView.frame, visibleRect) ||
CGRectIsNull(visibleRect));
if(drawerFullyCovered){
[self prepareToPresentDrawer:drawerSide animated:animated];
}

if(sideDrawerViewController){
self.animatingDrawer = animated;
CGRect newFrame;
CGRect oldFrame = self.centerContainerView.frame;
if(drawerSide == MMDrawerSideLeft){
newFrame = self.centerContainerView.frame;
newFrame.origin.x = self.maximumLeftDrawerWidth;
}
else {
newFrame = self.centerContainerView.frame;
newFrame.origin.x = 0-self.maximumRightDrawerWidth;
}

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

[UIView
animateWithDuration:(animated?duration:0.0)
delay:0.0
options:options
animations:^{
[self.centerContainerView setFrame:newFrame];
[self updateDrawerVisualStateForDrawerSide:drawerSide percentVisible:1.0];
}
completion:^(BOOL finished) {
//End the appearance transition if it already wasn't open.
if(drawerSide != self.openSide){
[sideDrawerViewController endAppearanceTransition];
}
[self setOpenSide:drawerSide];

[self resetDrawerVisualStateForDrawerSide:drawerSide];
[self setAnimatingDrawer:NO];
if(completion){
completion(finished);
}
}];
}
}
}

#pragma mark - Updating the Center View Controller
-(void)setCenterViewController:(UIViewController *)centerViewController animated:(BOOL)animated{
if(_centerContainerView == nil){
Expand Down