Skip to content
This repository was archived by the owner on Jul 12, 2019. It is now read-only.

Sticky Menu (earlier: Fixed broken snapshots for cells with background views) #65

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added MCSwipeTableViewCellModeSticky mode to allow for a sticky menu …
…(e.g. Apple Mail-style "Archive" button).
  • Loading branch information
benjaminjackson committed May 3, 2014
commit 340e1a4433b2ca8139816ec0cc1618e2094a28cd
32 changes: 24 additions & 8 deletions MCSwipe Demo/MCSwipe Demo/MCTableViewController.m
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
#import "MCSwipeTableViewCell.h"
#import "MCTableViewController.h"

static NSUInteger const kMCNumItems = 7;
static NSUInteger const kMCNumItems = 8;

@interface MCTableViewController () <MCSwipeTableViewCellDelegate, UIAlertViewDelegate>

@@ -131,6 +131,16 @@ - (void)configureCell:(MCSwipeTableViewCell *)cell forRowAtIndexPath:(NSIndexPat
}

else if (indexPath.row % kMCNumItems == 2) {
[cell.textLabel setText:@"Sticky Mode Cell"];
[cell.detailTextLabel setText:@"Swipe to open menu"];
checkView.frame = CGRectMake(0, 0, 80, 70);
checkView.backgroundColor = greenColor;
[cell setSwipeGestureWithView:checkView color:greenColor mode:MCSwipeTableViewCellModeSticky state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {
NSLog(@"Did swipe \"Checkmark\" cell");
}];
}

else if (indexPath.row % kMCNumItems == 3) {
[cell.textLabel setText:@"Mixed Mode Cell"];
[cell.detailTextLabel setText:@"Swipe to switch or delete"];
cell.shouldAnimateIcons = YES;
@@ -146,7 +156,7 @@ - (void)configureCell:(MCSwipeTableViewCell *)cell forRowAtIndexPath:(NSIndexPat
}];
}

else if (indexPath.row % kMCNumItems == 3) {
else if (indexPath.row % kMCNumItems == 4) {
[cell.textLabel setText:@"Un-animated Icons"];
[cell.detailTextLabel setText:@"Swipe"];
cell.shouldAnimateIcons = NO;
@@ -162,7 +172,7 @@ - (void)configureCell:(MCSwipeTableViewCell *)cell forRowAtIndexPath:(NSIndexPat
}];
}

else if (indexPath.row % kMCNumItems == 4) {
else if (indexPath.row % kMCNumItems == 5) {
[cell.textLabel setText:@"Right swipe only"];
[cell.detailTextLabel setText:@"Swipe"];

@@ -175,7 +185,7 @@ - (void)configureCell:(MCSwipeTableViewCell *)cell forRowAtIndexPath:(NSIndexPat
}];
}

else if (indexPath.row % kMCNumItems == 5) {
else if (indexPath.row % kMCNumItems == 6) {
[cell.textLabel setText:@"Small triggers"];
[cell.detailTextLabel setText:@"Using 10% and 50%"];
cell.firstTrigger = 0.1;
@@ -192,7 +202,7 @@ - (void)configureCell:(MCSwipeTableViewCell *)cell forRowAtIndexPath:(NSIndexPat
}];
}

else if (indexPath.row % kMCNumItems == 6) {
else if (indexPath.row % kMCNumItems == 7) {
[cell.textLabel setText:@"Exit Mode Cell + Confirmation"];
[cell.detailTextLabel setText:@"Swipe to delete"];

@@ -257,9 +267,15 @@ - (void)deleteCell:(MCSwipeTableViewCell *)cell {

- (UIView *)viewWithImageName:(NSString *)imageName {
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeCenter;
return imageView;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, 70.0)];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.contentMode = UIViewContentModeCenter;
return button;
}

- (void)buttonPressed:(id)sender {
NSLog(@"Button pressed");
}

#pragma mark - UIAlertViewDelegate
9 changes: 6 additions & 3 deletions MCSwipeTableViewCell/MCSwipeTableViewCell.h
Original file line number Diff line number Diff line change
@@ -34,11 +34,14 @@ typedef NS_ENUM(NSUInteger, MCSwipeTableViewCellMode) {
/** Disabled swipe. */
MCSwipeTableViewCellModeNone = 0,

/** Upon swipe the cell if exited from the view. Useful for destructive actions. */
/** Upon swipe the cell is exited from the view. Useful for destructive actions. */
MCSwipeTableViewCellModeExit,

/** Upon swipe the cell if automatically swiped back to it's initial position. */
MCSwipeTableViewCellModeSwitch
/** Upon swipe the cell is automatically swiped back to it's initial position. */
MCSwipeTableViewCellModeSwitch,

/** Upon swipe the cell is automatically swiped back to the width of the view for the swipe gesture. */
MCSwipeTableViewCellModeSticky
};

/**
34 changes: 29 additions & 5 deletions MCSwipeTableViewCell/MCSwipeTableViewCell.m
Original file line number Diff line number Diff line change
@@ -326,11 +326,16 @@ - (void)handlePanGestureRecognizer:(UIPanGestureRecognizer *)gesture {
[self moveWithDuration:animationDuration andDirection:_direction];
}

else {
else if (cellMode == MCSwipeTableViewCellModeSwitch) {
[self swipeToOriginWithCompletion:^{
[self executeCompletionBlock];
}];
}
else {
[self swipeToStickyPositionWithCompletion:^{
[self executeCompletionBlock];
}];
}

// We notify the delegate that we just ended swiping.
if ([_delegate respondsToSelector:@selector(swipeTableViewCellDidEndSwiping:)]) {
@@ -612,27 +617,46 @@ - (void)moveWithDuration:(NSTimeInterval)duration andDirection:(MCSwipeTableView
}];
}

- (void)swipeToStickyPositionWithCompletion:(void(^)(void))completion {
self.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *currentView = [self viewWithPercentage:self.currentPercentage];
[self swipeToOriginWithCompletion:completion offset:CGRectGetWidth(currentView.frame)];
}

- (void)swipeToOriginWithCompletion:(void(^)(void))completion {
[self swipeToOriginWithCompletion:completion offset:0];
}

- (void)swipeToOriginWithCompletion:(void(^)(void))completion offset:(CGFloat)offset {
CGFloat bounceDistance = kMCBounceAmplitude * _currentPercentage;

if ([UIView.class respondsToSelector:@selector(animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:)]) {

[UIView animateWithDuration:_animationDuration delay:0.0 usingSpringWithDamping:_damping initialSpringVelocity:_velocity options:UIViewAnimationOptionCurveEaseInOut animations:^{

CGRect frame = _contentScreenshotView.frame;
frame.origin.x = 0;
if (self.currentPercentage >= 0) {
frame.origin.x = offset;
}
else {
frame.origin.x = -1 * offset;
}
_contentScreenshotView.frame = frame;

// Clearing the indicator view
_colorIndicatorView.backgroundColor = self.defaultColor;
if (offset == 0) {
_colorIndicatorView.backgroundColor = self.defaultColor;
_slidingView.alpha = 0;
}

_slidingView.alpha = 0;
[self slideViewWithPercentage:0 view:_activeView isDragging:NO];

} completion:^(BOOL finished) {

_isExited = NO;
[self uninstallSwipingView];
if (offset == 0) {
[self uninstallSwipingView];
}

if (completion) {
completion();