-
Notifications
You must be signed in to change notification settings - Fork 53
/
ZKRevealingTableViewCell.m
402 lines (316 loc) · 13.9 KB
/
ZKRevealingTableViewCell.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//
// ZKRevealingTableViewCell.m
// ZKRevealingTableViewCell
//
// Created by Alex Zielenski on 4/29/12.
// Copyright (c) 2012 Alex Zielenski.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "ZKRevealingTableViewCell.h"
#import <QuartzCore/QuartzCore.h>
@interface ZKRevealingTableViewCell ()
@property (nonatomic, retain) UIPanGestureRecognizer *_panGesture;
@property (nonatomic, assign) CGFloat _initialTouchPositionX;
@property (nonatomic, assign) CGFloat _initialHorizontalCenter;
@property (nonatomic, assign) ZKRevealingTableViewCellDirection _lastDirection;
@property (nonatomic, assign) ZKRevealingTableViewCellDirection _currentDirection;
- (void)_slideInContentViewFromDirection:(ZKRevealingTableViewCellDirection)direction offsetMultiplier:(CGFloat)multiplier;
- (void)_slideOutContentViewInDirection:(ZKRevealingTableViewCellDirection)direction;
- (void)_pan:(UIPanGestureRecognizer *)panGesture;
- (void)_setRevealing:(BOOL)revealing;
- (CGFloat)_originalCenter;
- (CGFloat)_bounceMultiplier;
- (BOOL)_shouldDragLeft;
- (BOOL)_shouldDragRight;
- (BOOL)_shouldReveal;
@end
@implementation ZKRevealingTableViewCell
#pragma mark - Private Properties
@synthesize _panGesture;
@synthesize _initialTouchPositionX;
@synthesize _initialHorizontalCenter;
@synthesize _lastDirection;
@synthesize _currentDirection;
#pragma mark - Public Properties
@dynamic revealing;
@synthesize direction = _direction;
@synthesize delegate = _delegate;
@synthesize shouldBounce = _shouldBounce;
@synthesize pixelsToReveal = _pixelsToReveal;
@synthesize backView = _backView;
#pragma mark - Lifecycle
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.direction = ZKRevealingTableViewCellDirectionBoth;
self.shouldBounce = YES;
self.pixelsToReveal = 0;
self._panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_pan:)] autorelease];
self._panGesture.delegate = self;
[self addGestureRecognizer:self._panGesture];
self.contentView.backgroundColor = [UIColor clearColor];
UIView *backgroundView = [[[UIView alloc] initWithFrame:self.contentView.frame] autorelease];
backgroundView.backgroundColor = [UIColor clearColor];
self.backView = backgroundView;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.direction = ZKRevealingTableViewCellDirectionBoth;
self.shouldBounce = YES;
self.pixelsToReveal = 0;
self._panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_pan:)] autorelease];
self._panGesture.delegate = self;
[self addGestureRecognizer:self._panGesture];
self.contentView.backgroundColor = [UIColor clearColor];
UIView *backgroundView = [[[UIView alloc] initWithFrame:self.contentView.frame] autorelease];
backgroundView.backgroundColor = [UIColor clearColor];
self.backView = backgroundView;
}
return self;
}
- (void)dealloc
{
self._panGesture = nil;
self.backView = nil;
[super dealloc];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self addSubview:self.backView];
[self addSubview:self.contentView];
self.backView.frame = self.contentView.frame;
}
#pragma mark - Accessors
#import <objc/runtime.h>
static char BOOLRevealing;
- (BOOL)isRevealing
{
return [(NSNumber *)objc_getAssociatedObject(self, &BOOLRevealing) boolValue];
}
- (void)setRevealing:(BOOL)revealing
{
// Don't change the value if its already that value.
// Reveal unless the delegate says no
if (revealing == self.revealing ||
(revealing && self._shouldReveal))
return;
[self _setRevealing:revealing];
if (self.isRevealing)
[self _slideOutContentViewInDirection:(self.isRevealing) ? self._currentDirection : self._lastDirection];
else
[self _slideInContentViewFromDirection:(self.isRevealing) ? self._currentDirection : self._lastDirection offsetMultiplier:self._bounceMultiplier];
}
- (void)_setRevealing:(BOOL)revealing
{
[self willChangeValueForKey:@"isRevealing"];
objc_setAssociatedObject(self, &BOOLRevealing, [NSNumber numberWithBool:revealing], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"isRevealing"];
if (self.isRevealing && [self.delegate respondsToSelector:@selector(cellDidReveal:)])
[self.delegate cellDidReveal:self];
}
- (BOOL)_shouldReveal
{
// Conditions are checked in order
return (![self.delegate respondsToSelector:@selector(cellShouldReveal:)] || [self.delegate cellShouldReveal:self]);
}
#pragma mark - Handing Touch
- (void)_pan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self];
CGPoint currentTouchPoint = [recognizer locationInView:self];
CGPoint velocity = [recognizer velocityInView:self];
CGFloat originalCenter = self._originalCenter;
CGFloat currentTouchPositionX = currentTouchPoint.x;
CGFloat panAmount = self._initialTouchPositionX - currentTouchPositionX;
CGFloat newCenterPosition = self._initialHorizontalCenter - panAmount;
CGFloat centerX = self.contentView.center.x;
if (recognizer.state == UIGestureRecognizerStateBegan) {
// Set a baseline for the panning
self._initialTouchPositionX = currentTouchPositionX;
self._initialHorizontalCenter = self.contentView.center.x;
if ([self.delegate respondsToSelector:@selector(cellDidBeginPan:)])
[self.delegate cellDidBeginPan:self];
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
// If the pan amount is negative, then the last direction is left, and vice versa.
if (newCenterPosition - centerX < 0)
self._lastDirection = ZKRevealingTableViewCellDirectionLeft;
else
self._lastDirection = ZKRevealingTableViewCellDirectionRight;
// Don't let you drag past a certain point depending on direction
if ((newCenterPosition < originalCenter && !self._shouldDragLeft) || (newCenterPosition > originalCenter && !self._shouldDragRight))
newCenterPosition = originalCenter;
if (self.pixelsToReveal != 0) {
// Let's not go waaay out of bounds
if (newCenterPosition > originalCenter + self.pixelsToReveal)
newCenterPosition = originalCenter + self.pixelsToReveal;
else if (newCenterPosition < originalCenter - self.pixelsToReveal)
newCenterPosition = originalCenter - self.pixelsToReveal;
}else {
// Let's not go waaay out of bounds
if (newCenterPosition > self.bounds.size.width + originalCenter)
newCenterPosition = self.bounds.size.width + originalCenter;
else if (newCenterPosition < -originalCenter)
newCenterPosition = -originalCenter;
}
CGPoint center = self.contentView.center;
center.x = newCenterPosition;
self.contentView.layer.position = center;
} else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
// Swiping left, velocity is below 0.
// Swiping right, it is above 0
// If the velocity is above the width in points per second at any point in the pan, push it to the acceptable side
// Otherwise, if we are 60 points in, push to the other side
// If we are < 60 points in, bounce back
#define kMinimumVelocity self.contentView.frame.size.width
#define kMinimumPan 60.0
CGFloat velocityX = velocity.x;
BOOL push = (velocityX < -kMinimumVelocity);
push |= (velocityX > kMinimumVelocity);
push |= ((self._lastDirection == ZKRevealingTableViewCellDirectionLeft && translation.x < -kMinimumPan) || (self._lastDirection == ZKRevealingTableViewCellDirectionRight && translation.x > kMinimumPan));
push &= self._shouldReveal;
push &= ((self._lastDirection == ZKRevealingTableViewCellDirectionRight && self._shouldDragRight) || (self._lastDirection == ZKRevealingTableViewCellDirectionLeft && self._shouldDragLeft));
if (velocityX > 0 && self._lastDirection == ZKRevealingTableViewCellDirectionLeft)
push = NO;
else if (velocityX < 0 && self._lastDirection == ZKRevealingTableViewCellDirectionRight)
push = NO;
if (push && !self.isRevealing) {
[self _slideOutContentViewInDirection:self._lastDirection];
[self _setRevealing:YES];
self._currentDirection = self._lastDirection;
} else if (self.isRevealing && translation.x != 0) {
CGFloat multiplier = self._bounceMultiplier;
if (!self.isRevealing)
multiplier *= -1.0;
[self _slideInContentViewFromDirection:self._currentDirection offsetMultiplier:multiplier];
[self _setRevealing:NO];
} else if (translation.x != 0) {
// Figure out which side we've dragged on.
ZKRevealingTableViewCellDirection finalDir = ZKRevealingTableViewCellDirectionRight;
if (translation.x < 0)
finalDir = ZKRevealingTableViewCellDirectionLeft;
[self _slideInContentViewFromDirection:finalDir offsetMultiplier:-1.0 * self._bounceMultiplier];
[self _setRevealing:NO];
}
}
}
- (BOOL)_shouldDragLeft
{
return (self.direction == ZKRevealingTableViewCellDirectionBoth || self.direction == ZKRevealingTableViewCellDirectionLeft);
}
- (BOOL)_shouldDragRight
{
return (self.direction == ZKRevealingTableViewCellDirectionBoth || self.direction == ZKRevealingTableViewCellDirectionRight);
}
- (CGFloat)_originalCenter
{
return ceil(self.bounds.size.width / 2);
}
- (CGFloat)_bounceMultiplier
{
return self.shouldBounce ? MIN(ABS(self._originalCenter - self.contentView.center.x) / kMinimumPan, 1.0) : 0.0;
}
#pragma mark - Sliding
#define kBOUNCE_DISTANCE 7.0
- (void)_slideInContentViewFromDirection:(ZKRevealingTableViewCellDirection)direction offsetMultiplier:(CGFloat)multiplier
{
CGFloat bounceDistance;
if (self.contentView.center.x == self._originalCenter)
return;
switch (direction) {
case ZKRevealingTableViewCellDirectionRight:
bounceDistance = kBOUNCE_DISTANCE * multiplier;
break;
case ZKRevealingTableViewCellDirectionLeft:
bounceDistance = -kBOUNCE_DISTANCE * multiplier;
break;
default:
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unhandled gesture direction" userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:direction] forKey:@"direction"]];
break;
}
[UIView animateWithDuration:0.1
delay:0
options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction
animations:^{ self.contentView.center = CGPointMake(self._originalCenter, self.contentView.center.y); }
completion:^(BOOL f) {
[UIView animateWithDuration:0.1 delay:0
options:UIViewAnimationCurveEaseOut
animations:^{ self.contentView.frame = CGRectOffset(self.contentView.frame, bounceDistance, 0); }
completion:^(BOOL f) {
[UIView animateWithDuration:0.1 delay:0
options:UIViewAnimationCurveEaseIn
animations:^{ self.contentView.frame = CGRectOffset(self.contentView.frame, -bounceDistance, 0); }
completion:NULL];
}
];
}];
}
- (void)_slideOutContentViewInDirection:(ZKRevealingTableViewCellDirection)direction;
{
CGFloat x;
if (self.pixelsToReveal != 0) {
switch (direction) {
case ZKRevealingTableViewCellDirectionLeft:
x = self._originalCenter - self.pixelsToReveal;
break;
case ZKRevealingTableViewCellDirectionRight:
x = self._originalCenter + self.pixelsToReveal;
break;
default:
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unhandled gesture direction" userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:direction] forKey:@"direction"]];
break;
}
}
else {
switch (direction) {
case ZKRevealingTableViewCellDirectionLeft:
x = - self._originalCenter;
break;
case ZKRevealingTableViewCellDirectionRight:
x = self.contentView.frame.size.width + self._originalCenter;
break;
default:
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unhandled gesture direction" userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:direction] forKey:@"direction"]];
break;
}
}
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{ self.contentView.center = CGPointMake(x, self.contentView.center.y); }
completion:NULL];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self._panGesture) {
UIScrollView *superview = (UIScrollView *)self.superview;
CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:superview];
// Make sure it is scrolling horizontally
return ((fabs(translation.x) / fabs(translation.y) > 1) ? YES : NO && (superview.contentOffset.y == 0.0 && superview.contentOffset.x == 0.0));
}
return NO;
}
@end