forked from CEWendel/SWTableViewCell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSWTableViewCell.m
More file actions
614 lines (512 loc) · 21.5 KB
/
Copy pathSWTableViewCell.m
File metadata and controls
614 lines (512 loc) · 21.5 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//
// SWTableViewCell.m
// SWTableViewCell
//
// Created by Chris Wendel on 9/10/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "SWTableViewCell.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#import "SWUtilityButtonView.h"
static NSString * const kTableViewCellContentView = @"UITableViewCellContentView";
#pragma mark - SWUtilityButtonView
@interface SWTableViewCell () <UIScrollViewDelegate>
{
SWCellState _cellState; // The state of the cell within the scroll view, can be left, right or middle
CGFloat additionalRightPadding;
dispatch_once_t onceToken;
}
@property (nonatomic, strong) SWUtilityButtonView *scrollViewButtonViewLeft;
@property (nonatomic, strong) SWUtilityButtonView *scrollViewButtonViewRight;
@property (nonatomic, weak) UIView *scrollViewContentView;
@property (nonatomic) CGFloat height;
@property (nonatomic, strong) SWLongPressGestureRecognizer *longPressGestureRecognizer;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
@end
@implementation SWTableViewCell
#pragma mark Initializers
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier containingTableView:(UITableView *)containingTableView leftUtilityButtons:(NSArray *)leftUtilityButtons rightUtilityButtons:(NSArray *)rightUtilityButtons
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self.height = containingTableView.rowHeight;
self.containingTableView = containingTableView;
self.highlighted = NO;
[self initializer];
self.rightUtilityButtons = rightUtilityButtons;
self.leftUtilityButtons = leftUtilityButtons;
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self initializer];
}
return self;
}
- (instancetype)init
{
self = [super init];
if (self)
{
[self initializer];
}
return self;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
[self initializer];
}
return self;
}
- (void)initializer
{
// Check if the UITableView will display Indices on the right. If that's the case, add a padding
if([self.containingTableView.dataSource respondsToSelector:@selector(sectionIndexTitlesForTableView:)])
{
NSArray *indices = [self.containingTableView.dataSource sectionIndexTitlesForTableView:self.containingTableView];
additionalRightPadding = indices == nil ? 0 : kSectionIndexWidth;
}
// Set up scroll view that will host our cell content
SWCellScrollView *cellScrollView = [[SWCellScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), self.height)];
cellScrollView.delegate = self;
cellScrollView.showsHorizontalScrollIndicator = NO;
cellScrollView.scrollsToTop = NO;
cellScrollView.scrollEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(scrollViewUp:)];
tapGestureRecognizer.cancelsTouchesInView = NO;
[cellScrollView addGestureRecognizer:tapGestureRecognizer];
self.tapGestureRecognizer = tapGestureRecognizer;
SWLongPressGestureRecognizer *longPressGestureRecognizer = [[SWLongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(scrollViewPressed:)];
longPressGestureRecognizer.cancelsTouchesInView = NO;
longPressGestureRecognizer.minimumPressDuration = 0.1;
[cellScrollView addGestureRecognizer:longPressGestureRecognizer];
self.longPressGestureRecognizer = longPressGestureRecognizer;
self.cellScrollView = cellScrollView;
// Create the content view that will live in our scroll view
UIView *scrollViewContentView = [[UIView alloc] initWithFrame:CGRectMake([self leftUtilityButtonsWidth], 0, CGRectGetWidth(self.bounds), self.height)];
scrollViewContentView.backgroundColor = [UIColor whiteColor];
[self.cellScrollView addSubview:scrollViewContentView];
self.scrollViewContentView = scrollViewContentView;
// Add the cell scroll view to the cell
UIView *contentViewParent = self;
if (![NSStringFromClass([[self.subviews objectAtIndex:0] class]) isEqualToString:kTableViewCellContentView])
{
// iOS 7
contentViewParent = [self.subviews objectAtIndex:0];
}
NSArray *cellSubviews = [contentViewParent subviews];
[self insertSubview:cellScrollView atIndex:0];
for (UIView *subview in cellSubviews)
{
[self.scrollViewContentView addSubview:subview];
}
self.containingTableView.directionalLockEnabled = YES;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.cellScrollView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), self.height);
self.cellScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) + [self utilityButtonsPadding], self.height);
self.cellScrollView.contentOffset = CGPointMake([self leftUtilityButtonsWidth], 0);
self.scrollViewButtonViewLeft.frame = CGRectMake([self leftUtilityButtonsWidth], 0, 0, self.height);
self.scrollViewButtonViewLeft.layer.masksToBounds = YES;
self.scrollViewButtonViewRight.frame = CGRectMake(CGRectGetWidth(self.bounds), 0, 0, self.height);
self.scrollViewButtonViewRight.layer.masksToBounds = YES;
self.scrollViewContentView.frame = CGRectMake([self leftUtilityButtonsWidth], 0, CGRectGetWidth(self.bounds), self.height);
self.cellScrollView.scrollEnabled = YES;
self.tapGestureRecognizer.enabled = YES;
}
#pragma mark - Properties
- (void)setLeftUtilityButtons:(NSArray *)leftUtilityButtons
{
_leftUtilityButtons = leftUtilityButtons;
SWUtilityButtonView *departingLeftButtons = self.scrollViewButtonViewLeft;
SWUtilityButtonView *scrollViewButtonViewLeft = [[SWUtilityButtonView alloc] initWithUtilityButtons:leftUtilityButtons
parentCell:self
utilityButtonSelector:@selector(leftUtilityButtonHandler:)];
self.scrollViewButtonViewLeft = scrollViewButtonViewLeft;
[scrollViewButtonViewLeft setFrame:CGRectMake([self leftUtilityButtonsWidth], 0, [self leftUtilityButtonsWidth], self.height)];
[self.cellScrollView insertSubview:scrollViewButtonViewLeft belowSubview:self.scrollViewContentView];
[departingLeftButtons removeFromSuperview];
[scrollViewButtonViewLeft populateUtilityButtons];
[self setNeedsLayout];
}
- (void)setRightUtilityButtons:(NSArray *)rightUtilityButtons
{
_rightUtilityButtons = rightUtilityButtons;
SWUtilityButtonView *departingLeftButtons = self.scrollViewButtonViewRight;
SWUtilityButtonView *scrollViewButtonViewRight = [[SWUtilityButtonView alloc] initWithUtilityButtons:rightUtilityButtons
parentCell:self
utilityButtonSelector:@selector(rightUtilityButtonHandler:)];
self.scrollViewButtonViewRight = scrollViewButtonViewRight;
[scrollViewButtonViewRight setFrame:CGRectMake(CGRectGetWidth(self.bounds), 0, [self rightUtilityButtonsWidth], self.height)];
[self.cellScrollView insertSubview:scrollViewButtonViewRight belowSubview:self.scrollViewContentView];
[departingLeftButtons removeFromSuperview];
[scrollViewButtonViewRight populateUtilityButtons];
[self setNeedsLayout];
}
#pragma mark Selection
- (void)scrollViewPressed:(id)sender
{
SWLongPressGestureRecognizer *longPressGestureRecognizer = (SWLongPressGestureRecognizer *)sender;
if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
// Gesture recognizer ended without failing so we select the cell
[self selectCell];
// Set back to deselected
[self setSelected:NO];
}
else
{
// Handle the highlighting of the cell
if (self.isHighlighted)
{
[self setHighlighted:NO];
}
else
{
[self highlightCell];
}
}
}
- (void)scrollViewUp:(id)sender
{
[self selectCellWithTimedHighlight];
}
- (void)selectCell
{
if (_cellState == kCellStateCenter)
{
if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
{
NSIndexPath *cellIndexPath = [self.containingTableView indexPathForCell:self];
[self.containingTableView selectRowAtIndexPath:cellIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self.containingTableView.delegate tableView:self.containingTableView didSelectRowAtIndexPath:cellIndexPath];
[self.containingTableView deselectRowAtIndexPath:cellIndexPath animated:NO];
}
}
}
- (void)selectCellWithTimedHighlight
{
if(_cellState == kCellStateCenter)
{
// Selection
if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
{
NSIndexPath *cellIndexPath = [self.containingTableView indexPathForCell:self];
[self setSelected:YES];
[self.containingTableView selectRowAtIndexPath:cellIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self.containingTableView.delegate tableView:self.containingTableView didSelectRowAtIndexPath:cellIndexPath];
[self.containingTableView deselectRowAtIndexPath:cellIndexPath animated:NO];
// Make the selection visible
NSTimer *endHighlightTimer = [NSTimer scheduledTimerWithTimeInterval:0.20
target:self
selector:@selector(timerEndCellHighlight:)
userInfo:nil
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:endHighlightTimer forMode:NSRunLoopCommonModes];
}
}
else
{
// Scroll back to center
[self hideUtilityButtonsAnimated:YES];
}
}
- (void)highlightCell
{
if (_cellState == kCellStateCenter)
{
[self setHighlighted:YES];
}
}
- (void)timerEndCellHighlight:(id)sender
{
[self setSelected:NO];
}
#pragma mark UITableViewCell overrides
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
[super setBackgroundColor:backgroundColor];
_scrollViewContentView.backgroundColor = backgroundColor;
}
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted animated:NO];
self.scrollViewButtonViewLeft.hidden = highlighted;
self.scrollViewButtonViewRight.hidden = highlighted;
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
self.scrollViewButtonViewLeft.hidden = highlighted;
self.scrollViewButtonViewRight.hidden = highlighted;
}
- (void)setSelected:(BOOL)selected
{
[self setHighlighted:selected];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[self setHighlighted:selected];
}
#pragma mark Height methods
- (void)setCellHeight:(CGFloat)height
{
_height = height;
// update the utility button height
[self.scrollViewButtonViewLeft setHeight:height];
[self.scrollViewButtonViewRight setHeight:height];
[self layoutSubviews];
}
#pragma mark - Utility buttons handling
- (void)rightUtilityButtonHandler:(id)sender
{
SWUtilityButtonTapGestureRecognizer *utilityButtonTapGestureRecognizer = (SWUtilityButtonTapGestureRecognizer *)sender;
NSUInteger utilityButtonIndex = utilityButtonTapGestureRecognizer.buttonIndex;
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:didTriggerRightUtilityButtonWithIndex:)])
{
[self.delegate swipeableTableViewCell:self didTriggerRightUtilityButtonWithIndex:utilityButtonIndex];
}
}
- (void)leftUtilityButtonHandler:(id)sender
{
SWUtilityButtonTapGestureRecognizer *utilityButtonTapGestureRecognizer = (SWUtilityButtonTapGestureRecognizer *)sender;
NSUInteger utilityButtonIndex = utilityButtonTapGestureRecognizer.buttonIndex;
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:didTriggerLeftUtilityButtonWithIndex:)])
{
[self.delegate swipeableTableViewCell:self didTriggerLeftUtilityButtonWithIndex:utilityButtonIndex];
}
}
- (void)hideUtilityButtonsAnimated:(BOOL)animated
{
// Scroll back to center
// Force the scroll back to run on the main thread because of weird scroll view bugs
dispatch_async(dispatch_get_main_queue(), ^{
[self.cellScrollView setContentOffset:CGPointMake([self leftUtilityButtonsWidth], 0) animated:YES];
});
_cellState = kCellStateCenter;
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateCenter];
}
}
#pragma mark - Setup helpers
- (CGFloat)leftUtilityButtonsWidth
{
return [self.scrollViewButtonViewLeft utilityButtonsWidth];
}
- (CGFloat)rightUtilityButtonsWidth
{
return [self.scrollViewButtonViewRight utilityButtonsWidth] + additionalRightPadding;
}
- (CGFloat)utilityButtonsPadding
{
return [self leftUtilityButtonsWidth] + [self rightUtilityButtonsWidth];
}
- (CGPoint)scrollViewContentOffset
{
return CGPointMake([self.scrollViewButtonViewLeft utilityButtonsWidth], 0);
}
- (void) setAppearanceWithBlock:(void (^)())appearanceBlock force:(BOOL)force
{
if (force)
{
appearanceBlock();
}
else
{
dispatch_once(&onceToken, ^{
appearanceBlock();
});
}
}
#pragma mark UIScrollView helpers
- (void)scrollToRight:(inout CGPoint *)targetContentOffset
{
targetContentOffset->x = [self utilityButtonsPadding];
_cellState = kCellStateRight;
self.longPressGestureRecognizer.enabled = NO;
self.tapGestureRecognizer.enabled = NO;
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateRight];
}
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:)])
{
for (SWTableViewCell *cell in [self.containingTableView visibleCells]) {
if (cell != self && [cell isKindOfClass:[SWTableViewCell class]] && [self.delegate swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:cell]) {
[cell hideUtilityButtonsAnimated:YES];
}
}
}
}
- (void)scrollToCenter:(inout CGPoint *)targetContentOffset
{
targetContentOffset->x = [self leftUtilityButtonsWidth];
_cellState = kCellStateCenter;
self.longPressGestureRecognizer.enabled = YES;
self.tapGestureRecognizer.enabled = NO;
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateCenter];
}
}
- (void)scrollToLeft:(inout CGPoint *)targetContentOffset
{
targetContentOffset->x = 0;
_cellState = kCellStateLeft;
self.longPressGestureRecognizer.enabled = NO;
self.tapGestureRecognizer.enabled = NO;
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateLeft];
}
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:)])
{
for (SWTableViewCell *cell in [self.containingTableView visibleCells]) {
if (cell != self && [cell isKindOfClass:[SWTableViewCell class]] && [self.delegate swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:cell]) {
[cell hideUtilityButtonsAnimated:YES];
}
}
}
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
switch (_cellState)
{
case kCellStateCenter:
if (velocity.x >= 0.5f)
{
[self scrollToRight:targetContentOffset];
}
else if (velocity.x <= -0.5f)
{
[self scrollToLeft:targetContentOffset];
}
else
{
CGFloat rightThreshold = [self utilityButtonsPadding] - ([self rightUtilityButtonsWidth] / 2);
CGFloat leftThreshold = [self leftUtilityButtonsWidth] / 2;
if (targetContentOffset->x > rightThreshold)
[self scrollToRight:targetContentOffset];
else if (targetContentOffset->x < leftThreshold)
[self scrollToLeft:targetContentOffset];
else
[self scrollToCenter:targetContentOffset];
}
break;
case kCellStateLeft:
if (velocity.x >= 0.5f)
{
[self scrollToCenter:targetContentOffset];
}
else if (velocity.x <= -0.5f)
{
// No-op
}
else
{
if (targetContentOffset->x >= ([self utilityButtonsPadding] - [self rightUtilityButtonsWidth] / 2))
[self scrollToRight:targetContentOffset];
else if (targetContentOffset->x > [self leftUtilityButtonsWidth] / 2)
[self scrollToCenter:targetContentOffset];
else
[self scrollToLeft:targetContentOffset];
}
break;
case kCellStateRight:
if (velocity.x >= 0.5f)
{
// No-op
}
else if (velocity.x <= -0.5f)
{
[self scrollToCenter:targetContentOffset];
}
else
{
if (targetContentOffset->x <= [self leftUtilityButtonsWidth] / 2)
[self scrollToLeft:targetContentOffset];
else if (targetContentOffset->x < ([self utilityButtonsPadding] - [self rightUtilityButtonsWidth] / 2))
[self scrollToCenter:targetContentOffset];
else
[self scrollToRight:targetContentOffset];
}
break;
default:
break;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.tapGestureRecognizer.enabled = NO;
if (scrollView.contentOffset.x > [self leftUtilityButtonsWidth])
{
if ([self rightUtilityButtonsWidth] > 0)
{
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCell:canSwipeToState:)])
{
scrollView.scrollEnabled = [self.delegate swipeableTableViewCell:self canSwipeToState:kCellStateRight];
if (!scrollView.scrollEnabled)
{
return;
}
}
CGFloat scrollViewWidth = MIN(scrollView.contentOffset.x - [self leftUtilityButtonsWidth], [self rightUtilityButtonsWidth]);
// Expose the right button view
self.scrollViewButtonViewRight.frame = CGRectMake(scrollView.contentOffset.x + (CGRectGetWidth(self.bounds) - scrollViewWidth), 0.0f, scrollViewWidth,self.height);
CGRect scrollViewBounds = self.scrollViewButtonViewRight.bounds;
scrollViewBounds.origin.x = MAX([self rightUtilityButtonsWidth] - scrollViewWidth, [self rightUtilityButtonsWidth] - scrollView.contentOffset.x);
self.scrollViewButtonViewRight.bounds = scrollViewBounds;
}
}
else
{
// Expose the left button view
if ([self leftUtilityButtonsWidth] > 0)
{
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCell:canSwipeToState:)])
{
scrollView.scrollEnabled = [self.delegate swipeableTableViewCell:self canSwipeToState:kCellStateLeft];
if (!scrollView.scrollEnabled)
{
return;
}
}
CGFloat scrollViewWidth = MIN(scrollView.contentOffset.x - [self leftUtilityButtonsWidth], [self leftUtilityButtonsWidth]);
self.scrollViewButtonViewLeft.frame = CGRectMake([self leftUtilityButtonsWidth], 0.0f, scrollViewWidth, self.height);
}
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.tapGestureRecognizer.enabled = YES;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
// Called when setContentOffset in hideUtilityButtonsAnimated: is done
self.tapGestureRecognizer.enabled = YES;
if (_cellState == kCellStateCenter)
{
self.longPressGestureRecognizer.enabled = YES;
}
}
#pragma mark - Cleaning up
- (void)dealloc
{
self.cellScrollView.delegate = nil;
self.cellScrollView = nil;
}
@end