Skip to content

Commit

Permalink
Add autoHideOnSwipe property to close the current cell when another c…
Browse files Browse the repository at this point in the history
…ell is swiped open
  • Loading branch information
Stephanie Sharp committed Jan 29, 2015
1 parent adb7ee5 commit f273d00
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
7 changes: 7 additions & 0 deletions MGSwipeTableCell/MGSwipeTableCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ typedef NS_ENUM(NSInteger, MGSwipeState) {
/** Property to read or change the current swipe offset programmatically */
@property (nonatomic, assign) CGFloat swipeOffset;

/** Readonly property that determines whether the left or right buttons are visible */
@property (nonatomic, readonly) BOOL visibleButtons;
/** Optional property that causes the current cell to close when another cell is swiped open.
* If not set, the user must tap to close the current cell then swipe to open another.
*/
@property (nonatomic, assign) BOOL autoHideOnSwipe;

/** Utility methods to show or hide swipe buttons programmatically */
-(void) hideSwipeAnimated: (BOOL) animated;
-(void) showSwipe: (MGSwipeDirection) direction animated: (BOOL) animated;
Expand Down
62 changes: 49 additions & 13 deletions MGSwipeTableCell/MGSwipeTableCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ -(instancetype) init

static NSMutableSet * singleSwipePerTable;

@interface MGSwipeTableCell ()
@property (nonatomic, readwrite) BOOL visibleButtons;
@end

@implementation MGSwipeTableCell
{
UITapGestureRecognizer * _tapRecognizer;
Expand Down Expand Up @@ -526,7 +530,7 @@ -(void) createSwipeViewIfNeeded

- (void) showSwipeOverlayIfNeeded
{
if (_tableInputOverlay) {
if (self.visibleButtons) {
return;
}
if (_swipeContentView)
Expand All @@ -535,12 +539,15 @@ - (void) showSwipeOverlayIfNeeded
_swipeOverlay.hidden = NO;
if (_swipeContentView)
[_swipeView addSubview:_swipeContentView];

//input overlay on the whole table
UITableView * table = [self parentTable];
_tableInputOverlay = [[MGSwipeTableInputOverlay alloc] initWithFrame:table.bounds];
_tableInputOverlay.currentCell = self;
[table addSubview:_tableInputOverlay];

if (!self.autoHideOnSwipe) {
//input overlay on the whole table
UITableView * table = [self parentTable];
_tableInputOverlay = [[MGSwipeTableInputOverlay alloc] initWithFrame:table.bounds];
_tableInputOverlay.currentCell = self;
[table addSubview:_tableInputOverlay];
}
self.visibleButtons = YES;

_previusSelectionStyle = self.selectionStyle;
self.selectionStyle = UITableViewCellSelectionStyleNone;
Expand All @@ -549,12 +556,18 @@ - (void) showSwipeOverlayIfNeeded
_tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:)];
_tapRecognizer.cancelsTouchesInView = YES;
_tapRecognizer.delegate = self;
[self addGestureRecognizer:_tapRecognizer];

if (self.autoHideOnSwipe) {
[[self parentTable] addGestureRecognizer:_tapRecognizer];
}
else {
[self addGestureRecognizer:_tapRecognizer];
}
}

-(void) hideSwipeOverlayIfNeeded
{
if (!_tableInputOverlay) {
if (!self.visibleButtons) {
return;
}

Expand All @@ -564,10 +577,13 @@ -(void) hideSwipeOverlayIfNeeded
[_swipeContentView removeFromSuperview];
[self.contentView addSubview:_swipeContentView];
}

[_tableInputOverlay removeFromSuperview];
_tableInputOverlay = nil;


if (!self.autoHideOnSwipe) {
[_tableInputOverlay removeFromSuperview];
_tableInputOverlay = nil;
}
self.visibleButtons = NO;

self.selectionStyle = _previusSelectionStyle;
[self setAccesoryViewsHidden:NO];

Expand Down Expand Up @@ -889,18 +905,38 @@ -(void) setSwipeOffset:(CGFloat)offset animated: (BOOL) animated completion:(voi
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)hideOtherCells
{
for (UITableViewCell *cell in [[self parentTable] visibleCells]) {
if ([cell isKindOfClass:[MGSwipeTableCell class]]) {
MGSwipeTableCell *swipeCell = (MGSwipeTableCell *)cell;
if (swipeCell.visibleButtons && swipeCell != self) {
[swipeCell hideSwipeAnimated:YES];
}
}
}
}

#pragma mark Gestures

-(void) tapHandler: (UITapGestureRecognizer *) recognizer
{
[self hideSwipeAnimated:YES];

if (self.autoHideOnSwipe) {
[self hideOtherCells];
}
}

-(void) panHandler: (UIPanGestureRecognizer *)gesture
{
CGPoint current = [gesture translationInView:self];

if (gesture.state == UIGestureRecognizerStateBegan) {
if (self.autoHideOnSwipe) {
[self hideOtherCells];
}

self.highlighted = NO;
self.selected = NO;
[self createSwipeViewIfNeeded];
Expand Down

0 comments on commit f273d00

Please sign in to comment.