-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathRMSwipeTableViewCell.m
191 lines (170 loc) · 9.02 KB
/
RMSwipeTableViewCell.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// RMSwipeTableViewCell.m
// RMSwipeTableView
//
// Created by Rune Madsen on 2012-11-24.
// Copyright (c) 2015 The App Boutique. All rights reserved.
//
#import "RMSwipeTableViewCell.h"
@implementation RMSwipeTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize
{
// We need to set the contentView's background color, otherwise the sides are clear on the swipe and animations
[self.contentView setBackgroundColor:[UIColor whiteColor]];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[panGestureRecognizer setDelegate:self];
[self addGestureRecognizer:panGestureRecognizer];
self.revealDirection = RMSwipeTableViewCellRevealDirectionBoth;
self.animationType = RMSwipeTableViewCellAnimationTypeBounce;
self.animationDuration = 0.2f;
self.shouldAnimateCellReset = YES;
self.backViewbackgroundColor = [UIColor colorWithWhite:0.92 alpha:1];
self.panElasticity = YES;
self.panElasticityFactor = 0.55f;
self.panElasticityStartingPoint = 0.0f;
UIView *backgroundView = [[UIView alloc] initWithFrame:self.frame];
backgroundView.backgroundColor = [UIColor whiteColor];
self.backgroundView = backgroundView;
}
-(void)prepareForReuse {
[super prepareForReuse];
self.shouldAnimateCellReset = YES;
}
#pragma mark - Gesture recognizer delegate
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
// We only want to deal with the gesture of it's a pan gesture
if ([panGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && self.revealDirection != RMSwipeTableViewCellRevealDirectionNone) {
CGPoint translation = [panGestureRecognizer translationInView:[self superview]];
return (fabs(translation.x) / fabs(translation.y) > 1) ? YES : NO;
} else {
return NO;
}
}
-(void)handlePanGesture:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint translation = [panGestureRecognizer translationInView:panGestureRecognizer.view];
CGPoint velocity = [panGestureRecognizer velocityInView:panGestureRecognizer.view];
CGFloat panOffset = translation.x;
if (self.panElasticity) {
if (ABS(translation.x) > self.panElasticityStartingPoint) {
CGFloat width = CGRectGetWidth(self.frame);
CGFloat offset = fabs(translation.x);
panOffset = (offset * self.panElasticityFactor * width) / (offset * self.panElasticityFactor + width);
panOffset *= translation.x < 0 ? -1.0f : 1.0f;
if (self.panElasticityStartingPoint > 0) {
panOffset = translation.x > 0 ? panOffset + self.panElasticityStartingPoint / 2 : panOffset - self.panElasticityStartingPoint / 2;
}
}
}
CGPoint actualTranslation = CGPointMake(panOffset, translation.y);
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan && [panGestureRecognizer numberOfTouches] > 0) {
[self didStartSwiping];
[self animateContentViewForPoint:actualTranslation velocity:velocity];
} else if (panGestureRecognizer.state == UIGestureRecognizerStateChanged && [panGestureRecognizer numberOfTouches] > 0) {
[self animateContentViewForPoint:actualTranslation velocity:velocity];
} else {
[self resetCellFromPoint:actualTranslation velocity:velocity];
}
}
-(void)didStartSwiping {
if ([self.delegate respondsToSelector:@selector(swipeTableViewCellDidStartSwiping:)]) {
[self.delegate swipeTableViewCellDidStartSwiping:self];
}
self.backView = [[UIView alloc] init];
self.backView.backgroundColor = self.backViewbackgroundColor;
self.backView.translatesAutoresizingMaskIntoConstraints = NO;
[self.backgroundView addSubview:self.backView];
[self.backgroundView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_backView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_backView)]];
[self.backgroundView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_backView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_backView)]];
}
#pragma mark - Gesture animations
-(void)animateContentViewForPoint:(CGPoint)point velocity:(CGPoint)velocity {
if ((point.x > 0 && self.revealDirection == RMSwipeTableViewCellRevealDirectionLeft) || (point.x < 0 && self.revealDirection == RMSwipeTableViewCellRevealDirectionRight) || self.revealDirection == RMSwipeTableViewCellRevealDirectionBoth) {
self.contentView.frame = CGRectOffset(self.contentView.bounds, point.x, 0);
if ([self.delegate respondsToSelector:@selector(swipeTableViewCell:didSwipeToPoint:velocity:)]) {
[self.delegate swipeTableViewCell:self didSwipeToPoint:point velocity:velocity];
}
} else if ((point.x > 0 && self.revealDirection == RMSwipeTableViewCellRevealDirectionRight) || (point.x < 0 && self.revealDirection == RMSwipeTableViewCellRevealDirectionLeft)) {
self.contentView.frame = CGRectOffset(self.contentView.bounds, 0, 0);
}
}
-(void)resetCellFromPoint:(CGPoint)point velocity:(CGPoint)velocity {
if ([self.delegate respondsToSelector:@selector(swipeTableViewCellWillResetState:fromPoint:animation:velocity:)]) {
[self.delegate swipeTableViewCellWillResetState:self fromPoint:point animation:self.animationType velocity:velocity];
}
if (self.shouldAnimateCellReset == NO) {
return;
}
if ((self.revealDirection == RMSwipeTableViewCellRevealDirectionLeft && point.x < 0) || (self.revealDirection == RMSwipeTableViewCellRevealDirectionRight && point.x > 0)) {
return;
}
if (self.animationType == RMSwipeTableViewCellAnimationTypeBounce) {
void (^completionBlock)(BOOL) = ^(BOOL finished) {
BOOL shouldCleanupBackView = YES;
if ([self.delegate respondsToSelector:@selector(swipeTableViewCellShouldCleanupBackView:)]) {
shouldCleanupBackView = [self.delegate swipeTableViewCellShouldCleanupBackView:self];
}
if (shouldCleanupBackView) {
[self cleanupBackView];
}
if ([self.delegate respondsToSelector:@selector(swipeTableViewCellDidResetState:fromPoint:animation:velocity:)]) {
[self.delegate swipeTableViewCellDidResetState:self fromPoint:point animation:self.animationType velocity:velocity];
}
};
[UIView animateWithDuration:self.animationDuration
delay:0
usingSpringWithDamping:0.7
initialSpringVelocity:point.x / 5
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.contentView.frame = self.contentView.bounds;
}
completion:completionBlock];
} else {
[UIView animateWithDuration:self.animationDuration
delay:0
options:(UIViewAnimationOptions)self.animationType
animations:^{
self.contentView.frame = CGRectOffset(self.contentView.bounds, 0, 0);
}
completion:^(BOOL finished) {
BOOL shouldCleanupBackView = YES;
if ([self.delegate respondsToSelector:@selector(swipeTableViewCellShouldCleanupBackView:)]) {
shouldCleanupBackView = [self.delegate swipeTableViewCellShouldCleanupBackView:self];
}
if (shouldCleanupBackView) {
[self cleanupBackView];
}
if ([self.delegate respondsToSelector:@selector(swipeTableViewCellDidResetState:fromPoint:animation:velocity:)]) {
[self.delegate swipeTableViewCellDidResetState:self fromPoint:point animation:self.animationType velocity:velocity];
}
}
];
}
}
-(void)cleanupBackView {
[_backView removeFromSuperview];
_backView = nil;
}
@end