Skip to content

Commit

Permalink
Notify previous cancelled animations. Add finish parameter to animati…
Browse files Browse the repository at this point in the history
…on completion callbacks.
  • Loading branch information
MortimerGoro committed Mar 20, 2016
1 parent 29d5c0c commit 9ac9abc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions MGSwipeTableCell/MGSwipeTableCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ typedef NS_ENUM(NSInteger, MGSwipeEasingFunction) {

/** Utility methods to show or hide swipe buttons programmatically */
-(void) hideSwipeAnimated: (BOOL) animated;
-(void) hideSwipeAnimated: (BOOL) animated completion:(void(^)()) completion;
-(void) hideSwipeAnimated: (BOOL) animated completion:(void(^)(BOOL finished)) completion;
-(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated;
-(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated completion:(void(^)()) completion;
-(void) setSwipeOffset:(CGFloat)offset animated: (BOOL) animated completion:(void(^)()) completion;
-(void) setSwipeOffset:(CGFloat)offset animation: (MGSwipeAnimation *) animation completion:(void(^)()) completion;
-(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated completion:(void(^)(BOOL finished)) completion;
-(void) setSwipeOffset:(CGFloat)offset animated: (BOOL) animated completion:(void(^)(BOOL finished)) completion;
-(void) setSwipeOffset:(CGFloat)offset animation: (MGSwipeAnimation *) animation completion:(void(^)(BOOL finished)) completion;
-(void) expandSwipe: (MGSwipeDirection) direction animated: (BOOL) animated;

/** Refresh method to be used when you want to update the cell contents while the user is swiping */
Expand Down
24 changes: 12 additions & 12 deletions MGSwipeTableCell/MGSwipeTableCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ -(void) expandToOffset:(CGFloat) offset settings:(MGSwipeExpansionSettings*) set
_expansionLayout = settings.expansionLayout;

CGFloat duration = _fromLeft ? _cell.leftExpansion.animationDuration : _cell.rightExpansion.animationDuration;
[UIView animateWithDuration: duration animations:^{
[UIView animateWithDuration: duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
_expandedButton.hidden = NO;

if (_expansionLayout == MGSwipeExpansionLayoutCenter) {
Expand Down Expand Up @@ -241,7 +241,7 @@ -(void) endExpansionAnimated:(BOOL) animated
_backgroundColorCopy = nil;
}
CGFloat duration = _fromLeft ? _cell.leftExpansion.animationDuration : _cell.rightExpansion.animationDuration;
[UIView animateWithDuration: animated ? duration : 0.0 animations:^{
[UIView animateWithDuration: animated ? duration : 0.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
_container.frame = self.bounds;
if (_expansionLayout == MGSwipeExpansionLayoutCenter) {
_expandedButtonAnimated.frame = _expandedButtonBoundsCopy;
Expand Down Expand Up @@ -560,7 +560,7 @@ @implementation MGSwipeTableCell
BOOL _triggerStateChanges;

MGSwipeAnimationData * _animationData;
void (^_animationCompletion)();
void (^_animationCompletion)(BOOL finished);
CADisplayLink * _displayLink;
}

Expand Down Expand Up @@ -1023,7 +1023,7 @@ - (void)setSwipeOffset:(CGFloat) newOffset;
}
}

-(void) hideSwipeAnimated: (BOOL) animated completion:(void(^)()) completion
-(void) hideSwipeAnimated: (BOOL) animated completion:(void(^)(BOOL finished)) completion
{
MGSwipeAnimation * animation = animated ? (_swipeOffset > 0 ? _leftSwipeSettings.hideAnimation: _rightSwipeSettings.hideAnimation) : nil;
[self setSwipeOffset:0 animation:animation completion:completion];
Expand All @@ -1039,7 +1039,7 @@ -(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated
[self showSwipe:direction animated:animated completion:nil];
}

-(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated completion:(void(^)()) completion
-(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated completion:(void(^)(BOOL finished)) completion
{
[self createSwipeViewIfNeeded];
_allowSwipeLeftToRight = _leftButtons.count > 0;
Expand Down Expand Up @@ -1068,7 +1068,7 @@ -(void) expandSwipe: (MGSwipeDirection) direction animated: (BOOL) animated
if (buttonsView) {
__weak MGSwipeButtonsView * expansionView = direction == MGSwipeDirectionLeftToRight ? _leftView : _rightView;
__weak MGSwipeTableCell * weakself = self;
[self setSwipeOffset:buttonsView.bounds.size.width * s * expSetting.threshold * 2 animation:expSetting.triggerAnimation completion:^{
[self setSwipeOffset:buttonsView.bounds.size.width * s * expSetting.threshold * 2 animation:expSetting.triggerAnimation completion:^(BOOL finished){
[expansionView endExpansionAnimated:YES];
[weakself setSwipeOffset:0 animated:NO completion:nil];
}];
Expand All @@ -1093,25 +1093,25 @@ -(void) animationTick: (CADisplayLink *) timer
[timer invalidate];
_displayLink = nil;
if (_animationCompletion) {
_animationCompletion();
_animationCompletion(YES);
_animationCompletion = nil;
}
}
}
-(void) setSwipeOffset:(CGFloat)offset animated: (BOOL) animated completion:(void(^)()) completion
-(void) setSwipeOffset:(CGFloat)offset animated: (BOOL) animated completion:(void(^)(BOOL finished)) completion
{
MGSwipeAnimation * animation = animated ? [[MGSwipeAnimation alloc] init] : nil;
[self setSwipeOffset:offset animation:animation completion:completion];
}

-(void) setSwipeOffset:(CGFloat)offset animation: (MGSwipeAnimation *) animation completion:(void(^)()) completion
-(void) setSwipeOffset:(CGFloat)offset animation: (MGSwipeAnimation *) animation completion:(void(^)(BOOL finished)) completion
{
if (_displayLink) {
[_displayLink invalidate];
_displayLink = nil;
}
if (_animationCompletion) { //finish pending animation callback
_animationCompletion();
if (_animationCompletion) { //notify previous animation cancelled
_animationCompletion(NO);
_animationCompletion = nil;
}
if (offset !=0) {
Expand All @@ -1121,7 +1121,7 @@ -(void) setSwipeOffset:(CGFloat)offset animation: (MGSwipeAnimation *) animation
if (!animation) {
self.swipeOffset = offset;
if (completion) {
completion();
completion(YES);
}
return;
}
Expand Down

0 comments on commit 9ac9abc

Please sign in to comment.