forked from lukeredpath/LRSlidingTableViewCell
/
LRSlidingTableViewCell.m
153 lines (126 loc) · 4.92 KB
/
LRSlidingTableViewCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// SlidingTableCell.m
// SlidingTableCell
//
// Created by Luke Redpath on 26/05/2011.
// Copyright 2011 LJR Software Limited. All rights reserved.
//
#import "LRSlidingTableViewCell.h"
@interface LRSlidingTableViewCell ()
- (void)addSwipeGestureRecognizer:(UISwipeGestureRecognizerDirection)direction;
@end
@implementation LRSlidingTableViewCell
@synthesize delegate;
@synthesize swipeDirection;
@synthesize lastGestureRecognized;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
self.swipeDirection = LRSlidingTableViewCellSwipeDirectionRight;
/** Add a default empty background view to make it clear that it's all working */
UIView *defaultBackgroundView = [[UIView alloc] initWithFrame:self.contentView.frame];
defaultBackgroundView.backgroundColor = [UIColor darkGrayColor];
self.backgroundView = defaultBackgroundView;
[defaultBackgroundView release];
}
return self;
}
- (void)dealloc
{
[lastGestureRecognized release];
[super dealloc];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)gesture
{
[self setLastGestureRecognized:gesture];
[self slideOutContentView];
[self.delegate cellDidReceiveSwipe:self];
}
- (void)setSwipeDirection:(LRSlidingTableViewCellSwipeDirection)direction
{
swipeDirection = direction;
NSArray *existingGestures = [self gestureRecognizers];
for (UIGestureRecognizer *gesture in existingGestures) {
[self removeGestureRecognizer:gesture];
}
switch (swipeDirection) {
case LRSlidingTableViewCellSwipeDirectionLeft:
[self addSwipeGestureRecognizer:UISwipeGestureRecognizerDirectionLeft];
break;
case LRSlidingTableViewCellSwipeDirectionRight:
[self addSwipeGestureRecognizer:UISwipeGestureRecognizerDirectionRight];
break;
case LRSlidingTableViewCellSwipeDirectionBoth:
[self addSwipeGestureRecognizer:UISwipeGestureRecognizerDirectionLeft];
[self addSwipeGestureRecognizer:UISwipeGestureRecognizerDirectionRight];
default:
break;
}
}
- (void)addSwipeGestureRecognizer:(UISwipeGestureRecognizerDirection)direction;
{
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeGesture.direction = direction;
[self addGestureRecognizer:swipeGesture];
[swipeGesture release];
}
#pragma mark Sliding content view
#define kBOUNCE_DISTANCE 20.0
void LR_offsetView(UIView *view, CGFloat offsetX, CGFloat offsetY)
{
view.frame = CGRectOffset(view.frame, offsetX, offsetY);
}
- (void)slideOutContentView;
{
CGFloat offsetX;
switch (self.lastGestureRecognized.direction) {
case UISwipeGestureRecognizerDirectionLeft:
offsetX = -self.contentView.frame.size.width;
break;
case UISwipeGestureRecognizerDirectionRight:
offsetX = self.contentView.frame.size.width;
break;
default:
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unhandled gesture direction" userInfo:[NSDictionary dictionaryWithObject:self.lastGestureRecognized forKey:@"lastGestureRecognized"]];
break;
}
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{ LR_offsetView(self.contentView, offsetX, 0); }
completion:NULL];
}
- (void)slideInContentView;
{
CGFloat offsetX, bounceDistance;
switch (self.lastGestureRecognized.direction) {
case UISwipeGestureRecognizerDirectionLeft:
offsetX = self.contentView.frame.size.width;
bounceDistance = -kBOUNCE_DISTANCE;
break;
case UISwipeGestureRecognizerDirectionRight:
offsetX = -self.contentView.frame.size.width;
bounceDistance = kBOUNCE_DISTANCE;
break;
default:
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unhandled gesture direction" userInfo:[NSDictionary dictionaryWithObject:self.lastGestureRecognized forKey:@"lastGestureRecognized"]];
break;
}
[UIView animateWithDuration:0.1
delay:0
options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction
animations:^{ LR_offsetView(self.contentView, offsetX, 0); }
completion:^(BOOL f) {
[UIView animateWithDuration:0.1 delay:0
options:UIViewAnimationCurveLinear
animations:^{ LR_offsetView(self.contentView, bounceDistance, 0); }
completion:^(BOOL f) {
[UIView animateWithDuration:0.1 delay:0
options:UIViewAnimationCurveLinear
animations:^{ LR_offsetView(self.contentView, -bounceDistance, 0); }
completion:NULL];
}
];
}];
}
@end