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

Revert "[Snackbar] Fix glitchy snackbar dismissal animation" #1163

Merged
merged 1 commit into from Jan 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 47 additions & 57 deletions components/Snackbar/src/private/MDCSnackbarOverlayView.m
Expand Up @@ -54,6 +54,11 @@ @interface MDCSnackbarOverlayView ()
*/
@property(nonatomic) MDCSnackbarMessageView *snackbarView;

/**
Storage for a completion block that is waiting for a CAAnimation to finish.
*/
@property(nonatomic, copy) void (^pendingCompletionBlock)(void);

/**
The object which will notify us of changes in the keyboard position.
*/
Expand Down Expand Up @@ -83,16 +88,6 @@ @interface MDCSnackbarOverlayView ()
*/
@property(nonatomic) NSTimeInterval rotationDuration;

/**
The constraint used to pin the bottom of the snackbar to the bottom of the screen.
*/
@property(nonatomic) NSLayoutConstraint *snackbarOnscreenConstraint;

/**
The constraint used to pin the top of the snackbar to the bottom of the screen.
*/
@property(nonatomic) NSLayoutConstraint *snackbarOffscreenConstraint;

@end

@implementation MDCSnackbarOverlayView
Expand Down Expand Up @@ -277,26 +272,13 @@ - (void)setSnackbarView:(MDCSnackbarMessageView *)snackbarView {
}

// Always pin the snackbar to the bottom of the container.
_snackbarOnscreenConstraint = [NSLayoutConstraint constraintWithItem:snackbarView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:container
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-bottomMargin];
_snackbarOnscreenConstraint.active = NO; // snackbar starts off-screen.
_snackbarOnscreenConstraint.priority = UILayoutPriorityDefaultHigh;
[container addConstraint:_snackbarOnscreenConstraint];

_snackbarOffscreenConstraint = [NSLayoutConstraint constraintWithItem:snackbarView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:container
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-bottomMargin];
_snackbarOffscreenConstraint.active = YES;
[container addConstraint:_snackbarOffscreenConstraint];
[container addConstraint:[NSLayoutConstraint constraintWithItem:snackbarView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:container
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-bottomMargin]];

// Always limit the height of the snackbar.
[container
Expand Down Expand Up @@ -426,45 +408,40 @@ - (void)fadeInsnackbarView:(MDCSnackbarMessageView *)snackbarView
#pragma mark - Slide Animation

- (void)slideMessageView:(MDCSnackbarMessageView *)snackbarView
onscreen:(BOOL)onscreen
fromY:(CGFloat)fromY
toY:(CGFloat)toY
fromContentOpacity:(CGFloat)fromContentOpacity
toContentOpacity:(CGFloat)toContentOpacity
notificationFrame:(CGRect)notificationFrame
completion:(void (^)(void))completion {
// Prepare to move the snackbar.
_snackbarOnscreenConstraint.active = onscreen;
_snackbarOffscreenConstraint.active = !onscreen;
[_containingView setNeedsUpdateConstraints];
// Save off @c completion for when the CAAnimation completes.
self.pendingCompletionBlock = completion;

CAMediaTimingFunction *timingFunction =
[CAMediaTimingFunction mdc_functionWithType:MDCAnimationTimingFunctionEaseInOut];
[CATransaction begin];
[CATransaction setAnimationTimingFunction:timingFunction];

// We use UIView animation inside a CATransaction in order to use the custom animation curve.
[UIView animateWithDuration:MDCSnackbarTransitionDuration
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// Trigger snackbar animation.
[_containingView layoutIfNeeded];
} completion:^(BOOL finished) {
if (completion) {
completion();
}
}];

// Move the snackbar.
CABasicAnimation *translationAnimation =
[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
translationAnimation.duration = MDCSnackbarTransitionDuration;
translationAnimation.fromValue = @(fromY);
translationAnimation.toValue = @(toY);
translationAnimation.delegate = self;
translationAnimation.timingFunction =
[CAMediaTimingFunction mdc_functionWithType:MDCAnimationTimingFunctionEaseInOut];

[snackbarView.layer addAnimation:translationAnimation forKey:@"translation"];

[snackbarView animateContentOpacityFrom:fromContentOpacity
to:toContentOpacity
duration:MDCSnackbarTransitionDuration
timingFunction:timingFunction];
duration:translationAnimation.duration
timingFunction:translationAnimation.timingFunction];
[CATransaction commit];

// Notify the overlay system.
[self notifyOverlayChangeWithFrame:notificationFrame
duration:MDCSnackbarTransitionDuration
duration:translationAnimation.duration
curve:0
timingFunction:timingFunction];
timingFunction:translationAnimation.timingFunction];
}

- (void)slideInMessageView:(MDCSnackbarMessageView *)snackbarView
Expand All @@ -473,7 +450,8 @@ - (void)slideInMessageView:(MDCSnackbarMessageView *)snackbarView
[self triggerSnackbarLayoutChange];

[self slideMessageView:snackbarView
onscreen:YES
fromY:snackbarView.bounds.size.height + [self staticBottomMargin]
toY:0.0f
fromContentOpacity:0
toContentOpacity:1
notificationFrame:[self snackbarRectInScreenCoordinates]
Expand All @@ -486,13 +464,25 @@ - (void)slideOutMessageView:(MDCSnackbarMessageView *)snackbarView
[self triggerSnackbarLayoutChange];

[self slideMessageView:snackbarView
onscreen:NO
fromY:0.0f
toY:snackbarView.bounds.size.height + [self staticBottomMargin]
fromContentOpacity:1
toContentOpacity:0
notificationFrame:CGRectNull
completion:completion];
}

#pragma mark - CAAnimationDelegate

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
void (^block)(void) = self.pendingCompletionBlock;
self.pendingCompletionBlock = nil;

if (block) {
block();
}
}

#pragma mark - Keyboard Notifications

- (void)updatesnackbarPositionWithKeyboardUserInfo:(NSDictionary *)userInfo {
Expand Down