-
Notifications
You must be signed in to change notification settings - Fork 74
/
TISwipeableTableView.m
442 lines (318 loc) · 13 KB
/
TISwipeableTableView.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//
// TISwipeableTableView.m
// TISwipeableTableView
//
// Created by Tom Irving on 28/05/2010.
// Copyright 2010 Tom Irving. All rights reserved.
//
#import "TISwipeableTableView.h"
#import <QuartzCore/QuartzCore.h>
//==========================================================
// - TISwipeableTableViewController
//==========================================================
@interface TISwipeableTableViewController ()
@property (nonatomic, strong) NSIndexPath * indexOfVisibleBackView;
@end
@implementation TISwipeableTableViewController
@synthesize indexOfVisibleBackView = _indexOfVisibleBackView;
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return ([indexPath compare:_indexOfVisibleBackView] == NSOrderedSame) ? nil : indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self tableView:tableView hideVisibleBackView:YES];
}
- (BOOL)tableView:(UITableView *)tableView shouldSwipeCellAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView didSwipeCellAtIndexPath:(NSIndexPath *)indexPath {
[self tableView:tableView hideVisibleBackView:YES];
[self setIndexOfVisibleBackView:indexPath];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self tableView:(UITableView *)scrollView hideVisibleBackView:YES];
}
- (void)tableView:(UITableView *)tableView revealBackViewAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[self tableView:tableView hideVisibleBackView:animated];
if ([cell respondsToSelector:@selector(revealBackViewAnimated:)]){
[(TISwipeableTableViewCell *)cell revealBackViewAnimated:animated];
[self setIndexOfVisibleBackView:indexPath];
}
}
- (void)tableView:(UITableView *)tableView hideVisibleBackView:(BOOL)animated {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:_indexOfVisibleBackView];
if ([cell respondsToSelector:@selector(hideBackViewAnimated:)]){
[(TISwipeableTableViewCell *)cell hideBackViewAnimated:animated];
[self setIndexOfVisibleBackView:nil];
}
}
@end
//==========================================================
// - TISwipeableTableViewCell
//==========================================================
@interface UIView (FindSwipeableCellSuperview)
@property (nonatomic, readonly) TISwipeableTableViewCell * tisw_findSuperview;
@property (nonatomic, readonly) UITableView * tisw_findTableview;
@end
@implementation UIView (FindSwipeableCellSuperview)
- (TISwipeableTableViewCell *)tisw_findSuperview {
UIView * superview = self.superview;
if ([superview isKindOfClass:[TISwipeableTableViewCell class]]) return (TISwipeableTableViewCell *)superview;
else if (superview) return superview.tisw_findSuperview;
else return nil;
}
- (UITableView *)tisw_findTableview {
UIView * tableView = self.superview;
if ([tableView isKindOfClass:[UITableView class]]) return (UITableView *)tableView;
else if (tableView) return tableView.tisw_findTableview;
else return nil;
}
@end
@implementation TISwipeableTableViewCellView
- (void)drawRect:(CGRect)rect {
[self.tisw_findSuperview drawContentView:rect];
}
@end
@implementation TISwipeableTableViewCellBackView
- (void)drawRect:(CGRect)rect {
[self.tisw_findSuperview drawBackView:rect];
}
@end
@interface TISwipeableTableViewCell (Private)
- (void)initialSetup;
- (void)resetViews:(BOOL)animated;
- (CAAnimationGroup *)bounceAnimationWithHideDuration:(CGFloat)hideDuration initialXOrigin:(CGFloat)originalX;
@end
@implementation TISwipeableTableViewCell {
UIView * _contentView;
UITableViewCellSelectionStyle _oldStyle;
}
@synthesize contentView = _contentView;
@synthesize backView = _backView;
@synthesize contentViewMoving = _contentViewMoving;
@synthesize shouldBounce = _shouldBounce;
#pragma mark - Init / Overrides
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){
[self initialSetup];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])){
[self initialSetup];
}
return self;
}
- (void)initialSetup {
[self setBackgroundColor:[UIColor clearColor]];
_contentView = [[TISwipeableTableViewCellView alloc] initWithFrame:CGRectZero];
[_contentView setClipsToBounds:YES];
[_contentView setOpaque:YES];
UISwipeGestureRecognizer * swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellWasSwiped:)];
[swipeRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft |
UISwipeGestureRecognizerDirectionRight)];
[_contentView addGestureRecognizer:swipeRecognizer];
_backView = [[TISwipeableTableViewCellBackView alloc] initWithFrame:CGRectZero];
[_backView setOpaque:YES];
[_backView setClipsToBounds:YES];
[_backView setHidden:YES];
[self addSubview:_backView];
[self addSubview:_contentView];
_contentViewMoving = NO;
_shouldBounce = YES;
_oldStyle = self.selectionStyle;
}
- (void)prepareForReuse {
[self resetViews:NO];
[super prepareForReuse];
}
- (void)setFrame:(CGRect)aFrame {
[super setFrame:aFrame];
CGRect newBounds = self.bounds;
newBounds.size.height -= 1;
[_backView setFrame:newBounds];
[_contentView setFrame:newBounds];
}
- (void)setNeedsDisplay {
[super setNeedsDisplay];
if (!_contentView.hidden) [_contentView setNeedsDisplay];
if (!_backView.hidden) [_backView setNeedsDisplay];
}
- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType {
// Having an accessory buggers swiping right up, so we override.
// It's easier just to draw the accessory yourself.
}
- (void)setAccessoryView:(UIView *)accessoryView {
// Same as above.
}
- (void)setHighlighted:(BOOL)highlighted {
[self setHighlighted:highlighted animated:NO];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
[self setNeedsDisplay];
}
- (void)setSelected:(BOOL)flag {
[self setSelected:flag animated:NO];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
[self setNeedsDisplay];
}
#pragma mark - Subclass Methods
// Implement the following in a subclass
- (void)drawContentView:(CGRect)rect {
}
- (void)drawBackView:(CGRect)rect {
}
// Optional implementation
- (void)backViewWillAppear:(BOOL)animated {
}
- (void)backViewDidAppear:(BOOL)animated {
}
- (void)backViewWillDisappear:(BOOL)animated {
}
- (void)backViewDidDisappear:(BOOL)animated {
}
//===============================//
#pragma mark - Back View Show / Hide
- (void)cellWasSwiped:(UISwipeGestureRecognizer *)recognizer {
UITableView * tableView = self.tisw_findTableview;
id delegate = tableView.nextResponder; // Hopefully this is a TISwipeableTableViewController.
if(![delegate isKindOfClass:[TISwipeableTableViewController class]])
delegate = [delegate nextResponder];
if ([delegate respondsToSelector:@selector(tableView:shouldSwipeCellAtIndexPath:)]){
NSIndexPath * myIndexPath = [tableView indexPathForCell:self];
if ([delegate tableView:tableView shouldSwipeCellAtIndexPath:myIndexPath]){
[self revealBackViewAnimated:YES];
if ([delegate respondsToSelector:@selector(tableView:didSwipeCellAtIndexPath:)]){
[delegate tableView:tableView didSwipeCellAtIndexPath:myIndexPath];
}
}
}
}
- (void)revealBackViewAnimated:(BOOL)animated {
if (!_contentViewMoving && _backView.hidden){
_contentViewMoving = YES;
[_backView.layer setHidden:NO];
[_backView setNeedsDisplay];
[self backViewWillAppear:animated];
_oldStyle = self.selectionStyle;
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
[_contentView.layer setAnchorPoint:CGPointMake(0, 0.5)];
[_contentView.layer setPosition:CGPointMake(_contentView.frame.size.width, _contentView.layer.position.y)];
if (animated){
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
[animation setRemovedOnCompletion:NO];
[animation setDelegate:self];
[animation setDuration:0.14];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[_contentView.layer addAnimation:animation forKey:@"reveal"];
}
else
{
[self backViewDidAppear:animated];
[self setSelected:NO];
_contentViewMoving = NO;
}
}
}
- (void)hideBackViewAnimated:(BOOL)animated {
if (!_backView.hidden){
_contentViewMoving = YES;
[self backViewWillDisappear:animated];
if (animated){
CGFloat hideDuration = 0.09;
[_backView.layer setOpacity:0.0];
CABasicAnimation * hideAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[hideAnimation setFromValue:[NSNumber numberWithFloat:1.0]];
[hideAnimation setToValue:[NSNumber numberWithFloat:0.0]];
[hideAnimation setDuration:hideDuration];
[hideAnimation setRemovedOnCompletion:NO];
[hideAnimation setDelegate:self];
[_backView.layer addAnimation:hideAnimation forKey:@"hide"];
CGFloat originalX = _contentView.layer.position.x;
[_contentView.layer setAnchorPoint:CGPointMake(0, 0.5)];
[_contentView.layer setPosition:CGPointMake(0, _contentView.layer.position.y)];
[_contentView.layer addAnimation:[self bounceAnimationWithHideDuration:hideDuration initialXOrigin:originalX]
forKey:@"bounce"];
}
else
{
[self resetViews:NO];
}
}
}
- (void)resetViews:(BOOL)animated {
[_contentView.layer removeAllAnimations];
[_backView.layer removeAllAnimations];
_contentViewMoving = NO;
[_contentView.layer setAnchorPoint:CGPointMake(0, 0.5)];
[_contentView.layer setPosition:CGPointMake(0, _contentView.layer.position.y)];
[_backView.layer setHidden:YES];
[_backView.layer setOpacity:1.0];
[self setSelectionStyle:_oldStyle];
[self backViewDidDisappear:animated];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if (anim == [_contentView.layer animationForKey:@"reveal"]){
[_contentView.layer removeAnimationForKey:@"reveal"];
[self backViewDidAppear:YES];
[self setSelected:NO];
_contentViewMoving = NO;
}
if (anim == [_contentView.layer animationForKey:@"bounce"]){
[_contentView.layer removeAnimationForKey:@"bounce"];
[self resetViews:YES];
}
if (anim == [_backView.layer animationForKey:@"hide"]){
[_backView.layer removeAnimationForKey:@"hide"];
}
}
- (CAAnimationGroup *)bounceAnimationWithHideDuration:(CGFloat)hideDuration initialXOrigin:(CGFloat)originalX {
CABasicAnimation * animation0 = [CABasicAnimation animationWithKeyPath:@"position.x"];
[animation0 setFromValue:[NSNumber numberWithFloat:originalX]];
[animation0 setToValue:[NSNumber numberWithFloat:0]];
[animation0 setDuration:hideDuration];
[animation0 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[animation0 setBeginTime:0];
CAAnimationGroup * hideAnimations = [CAAnimationGroup animation];
[hideAnimations setAnimations:[NSArray arrayWithObject:animation0]];
CGFloat fullDuration = hideDuration;
if (_shouldBounce){
CGFloat bounceDuration = 0.04;
CABasicAnimation * animation1 = [CABasicAnimation animationWithKeyPath:@"position.x"];
[animation1 setFromValue:[NSNumber numberWithFloat:0]];
[animation1 setToValue:[NSNumber numberWithFloat:-20]];
[animation1 setDuration:bounceDuration];
[animation1 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[animation1 setBeginTime:hideDuration];
CABasicAnimation * animation2 = [CABasicAnimation animationWithKeyPath:@"position.x"];
[animation2 setFromValue:[NSNumber numberWithFloat:-20]];
[animation2 setToValue:[NSNumber numberWithFloat:15]];
[animation2 setDuration:bounceDuration];
[animation2 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[animation2 setBeginTime:(hideDuration + bounceDuration)];
CABasicAnimation * animation3 = [CABasicAnimation animationWithKeyPath:@"position.x"];
[animation3 setFromValue:[NSNumber numberWithFloat:15]];
[animation3 setToValue:[NSNumber numberWithFloat:0]];
[animation3 setDuration:bounceDuration];
[animation3 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[animation3 setBeginTime:(hideDuration + (bounceDuration * 2))];
[hideAnimations setAnimations:[NSArray arrayWithObjects:animation0, animation1, animation2, animation3, nil]];
fullDuration = hideDuration + (bounceDuration * 3);
}
[hideAnimations setDuration:fullDuration];
[hideAnimations setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[hideAnimations setDelegate:self];
[hideAnimations setRemovedOnCompletion:NO];
return hideAnimations;
}
#pragma mark - Other
- (NSString *)description {
NSString * extraInfo = _backView.hidden ? @"ContentView visible": @"BackView visible";
return [NSString stringWithFormat:@"<TISwipeableTableViewCell %p; '%@'>", self, extraInfo];
}
@end