From 9ef7f07c89bcd66986814723e2707d970f3073df Mon Sep 17 00:00:00 2001 From: harishpathak Date: Wed, 29 Jun 2016 17:54:34 +0530 Subject: [PATCH] Hide on Left/Right Swipe with Animation We can now hide snack bar on swiping in left or right direction. --- Pod/Classes/SSSnackbar.m | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/Pod/Classes/SSSnackbar.m b/Pod/Classes/SSSnackbar.m index 03c5631..678903d 100644 --- a/Pod/Classes/SSSnackbar.m +++ b/Pod/Classes/SSSnackbar.m @@ -307,6 +307,54 @@ - (void)setupContentLayout { [self addConstraints:constraints]; [self layoutIfNeeded]; + + UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; + UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; + + // Setting the swipe direction. + [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; + [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; + + [self addGestureRecognizer:swipeLeft]; + [self addGestureRecognizer:swipeRight]; + +} + +-(void)handleSwipe:(UISwipeGestureRecognizer *)sender{ + if(sender.direction==UISwipeGestureRecognizerDirectionLeft){ + [self dismissSidewaysAnimated:YES]; + }else{ + [self dismissSidewaysAnimated:NO]; + } + } +- (void)dismissSidewaysAnimated:(BOOL)isLeft { + + [self invalidateTimer]; + currentlyVisibleSnackbar = nil; + + [UIView animateWithDuration:0.5 animations:^{ + CGRect frame = [self frame]; + if(isLeft){ + frame.origin.x = -self.frame.size.width; + + }else{ + frame.origin.x = 2*self.frame.size.width; + } + self.frame = frame; + + } completion:^(BOOL finished) { + [self.superview removeConstraints:self.visibleVerticalLayoutConstraints]; + [self.superview addConstraints:self.hiddenVerticalLayoutConstraints]; + [self.superview layoutIfNeeded]; + + if (!self.actionBlockDispatched) + [self executeDismissalBlock]; + [self removeFromSuperview]; + }]; + +} + + @end