-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSWTableViewCell.m
executable file
·463 lines (368 loc) · 17.5 KB
/
SWTableViewCell.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//
// SWTableViewCell.m
// SWTableViewCell
//
// Created by Chris Wendel on 9/10/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "SWTableViewCell.h"
#define kUtilityButtonsWidthMax 260
#define kUtilityButtonWidthDefault 90
static void *kTableViewCellObserverContext = &kTableViewCellObserverContext;
static NSString * const kTableViewCellContentView = @"UITableViewCellContentView";
#pragma mark - SWUtilityButtonView
@interface SWUtilityButtonView : UIView
@property (nonatomic, strong) NSArray *utilityButtons;
@property (nonatomic) CGFloat utilityButtonWidth;
@property (nonatomic, weak) SWTableViewCell *parentCell;
@property (nonatomic) SEL utilityButtonSelector;
- (id)initWithUtilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector;
- (id)initWithFrame:(CGRect)frame utilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector;
@end
@implementation SWUtilityButtonView
#pragma mark - SWUtilityButonView initializers
- (id)initWithUtilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector {
self = [super init];
if (self) {
self.utilityButtons = utilityButtons;
self.utilityButtonWidth = [self calculateUtilityButtonWidth];
self.parentCell = parentCell;
self.utilityButtonSelector = utilityButtonSelector;
}
return self;
}
- (id)initWithFrame:(CGRect)frame utilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector {
self = [super initWithFrame:frame];
if (self) {
self.utilityButtons = utilityButtons;
self.utilityButtonWidth = [self calculateUtilityButtonWidth];
self.parentCell = parentCell;
self.utilityButtonSelector = utilityButtonSelector;
}
return self;
}
#pragma mark Populating utility buttons
- (CGFloat)calculateUtilityButtonWidth {
CGFloat buttonWidth = kUtilityButtonWidthDefault;
if (buttonWidth * _utilityButtons.count > kUtilityButtonsWidthMax) {
CGFloat buffer = (buttonWidth * _utilityButtons.count) - kUtilityButtonsWidthMax;
buttonWidth -= (buffer / _utilityButtons.count);
}
return buttonWidth;
}
- (CGFloat)utilityButtonsWidth {
return (_utilityButtons.count * _utilityButtonWidth);
}
- (void)populateUtilityButtons {
NSUInteger utilityButtonsCounter = 0;
for (UIButton *utilityButton in _utilityButtons) {
CGFloat utilityButtonXCord = 0;
if (utilityButtonsCounter >= 1) utilityButtonXCord = _utilityButtonWidth * utilityButtonsCounter;
[utilityButton setFrame:CGRectMake(utilityButtonXCord, 0, _utilityButtonWidth, CGRectGetHeight(self.bounds))];
[utilityButton setTag:utilityButtonsCounter];
[utilityButton addTarget:_parentCell action:_utilityButtonSelector forControlEvents:UIControlEventTouchDown];
[self addSubview: utilityButton];
utilityButtonsCounter++;
}
}
@end
@interface SWTableViewCell () <UIScrollViewDelegate> {
//SWCellState _cellState; // The state of the cell within the scroll view, can be left, right or middle
}
// Scroll view to be added to UITableViewCell
@property (nonatomic, weak) UIScrollView *cellScrollView;
// The cell's height
@property (nonatomic) CGFloat height;
// Views that live in the scroll view
@property (nonatomic, weak) UIView *scrollViewContentView;
@property (nonatomic, strong) SWUtilityButtonView *scrollViewButtonViewLeft;
@property (nonatomic, strong) SWUtilityButtonView *scrollViewButtonViewRight;
// Used for row height and selection
@property (nonatomic, weak) UITableView *containingTableView;
@property (nonatomic, assign) SWCellState cellState;
@end
@implementation SWTableViewCell
#pragma mark Initializers
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier containingTableView:(UITableView *)containingTableView leftUtilityButtons:(NSArray *)leftUtilityButtons rightUtilityButtons:(NSArray *)rightUtilityButtons {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.rightUtilityButtons = rightUtilityButtons;
self.leftUtilityButtons = leftUtilityButtons;
self.height = containingTableView.rowHeight;
self.containingTableView = containingTableView;
self.highlighted = NO;
[self initializer];
[self.containingTableView addObserver:self forKeyPath:@"contentOffset" options:kNilOptions context:kTableViewCellObserverContext];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initializer];
}
return self;
}
- (id)init {
self = [super init];
if (self) {
[self initializer];
}
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initializer];
}
return self;
}
- (void)dealloc {
[self.containingTableView removeObserver:self forKeyPath:@"contentOffset" context:kTableViewCellObserverContext];
}
- (void)initializer {
// Set up scroll view that will host our cell content
UIScrollView *cellScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), _height)];
cellScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) + [self utilityButtonsPadding], _height);
cellScrollView.contentOffset = [self scrollViewContentOffset];
cellScrollView.delegate = self;
cellScrollView.showsHorizontalScrollIndicator = NO;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewPressed:)];
[cellScrollView addGestureRecognizer:tapGestureRecognizer];
self.cellScrollView = cellScrollView;
// Set up the views that will hold the utility buttons
SWUtilityButtonView *scrollViewButtonViewLeft = [[SWUtilityButtonView alloc] initWithUtilityButtons:_leftUtilityButtons parentCell:self utilityButtonSelector:@selector(leftUtilityButtonHandler:)];
[scrollViewButtonViewLeft setFrame:CGRectMake([self leftUtilityButtonsWidth], 0, [self leftUtilityButtonsWidth], _height)];
self.scrollViewButtonViewLeft = scrollViewButtonViewLeft;
[self.cellScrollView addSubview:scrollViewButtonViewLeft];
SWUtilityButtonView *scrollViewButtonViewRight = [[SWUtilityButtonView alloc] initWithUtilityButtons:_rightUtilityButtons parentCell:self utilityButtonSelector:@selector(rightUtilityButtonHandler:)];
[scrollViewButtonViewRight setFrame:CGRectMake(CGRectGetWidth(self.bounds), 0, [self rightUtilityButtonsWidth], _height)];
self.scrollViewButtonViewRight = scrollViewButtonViewRight;
[self.cellScrollView addSubview:scrollViewButtonViewRight];
// Populate the button views with utility buttons
[scrollViewButtonViewLeft populateUtilityButtons];
[scrollViewButtonViewRight populateUtilityButtons];
// Create the content view that will live in our scroll view
UIView *scrollViewContentView = [[UIView alloc] initWithFrame:CGRectMake([self leftUtilityButtonsWidth], 0, CGRectGetWidth(self.bounds), _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];
}
}
#pragma mark Selection
- (void)scrollViewPressed:(id)sender {
if(_cellState == kCellStateCenter) {
// Selection hack
if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]){
NSIndexPath *cellIndexPath = [_containingTableView indexPathForCell:self];
[self.containingTableView.delegate tableView:_containingTableView didSelectRowAtIndexPath:cellIndexPath];
}
// Highlight hack
if (!self.highlighted) {
self.scrollViewButtonViewLeft.hidden = YES;
self.scrollViewButtonViewRight.hidden = YES;
NSTimer *endHighlightTimer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(timerEndCellHighlight:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:endHighlightTimer forMode:NSRunLoopCommonModes];
[self setHighlighted:YES];
}
} else {
// Scroll back to center
[self hideUtilityButtonsAnimated:YES];
}
}
- (void)timerEndCellHighlight:(id)sender {
if (self.highlighted) {
self.scrollViewButtonViewLeft.hidden = NO;
self.scrollViewButtonViewRight.hidden = NO;
[self setHighlighted:NO];
}
}
#pragma mark UITableViewCell overrides
- (void)setBackgroundColor:(UIColor *)backgroundColor {
self.scrollViewContentView.backgroundColor = backgroundColor;
}
#pragma mark - Utility buttons handling
- (void)rightUtilityButtonHandler:(id)sender {
UIButton *utilityButton = (UIButton *)sender;
NSInteger utilityButtonTag = [utilityButton tag];
if ([_delegate respondsToSelector:@selector(swippableTableViewCell:didTriggerRightUtilityButtonWithIndex:)]) {
[_delegate swippableTableViewCell:self didTriggerRightUtilityButtonWithIndex:utilityButtonTag];
}
}
- (void)leftUtilityButtonHandler:(id)sender {
UIButton *utilityButton = (UIButton *)sender;
NSInteger utilityButtonTag = [utilityButton tag];
if ([_delegate respondsToSelector:@selector(swippableTableViewCell:didTriggerLeftUtilityButtonWithIndex:)]) {
[_delegate swippableTableViewCell:self didTriggerLeftUtilityButtonWithIndex:utilityButtonTag];
}
}
- (void)hideUtilityButtonsAnimated:(BOOL)animated {
// Scroll back to center
[self.cellScrollView setContentOffset:CGPointMake([self leftUtilityButtonsWidth], 0) animated:animated];
_cellState = kCellStateCenter;
if ([_delegate respondsToSelector:@selector(swippableTableViewCell:scrollingToState:)]) {
[_delegate swippableTableViewCell:self scrollingToState:kCellStateCenter];
}
}
#pragma mark - Overriden methods
- (void)layoutSubviews {
[super layoutSubviews];
self.cellScrollView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), _height);
self.cellScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) + [self utilityButtonsPadding], _height);
self.cellScrollView.contentOffset = CGPointMake([self leftUtilityButtonsWidth], 0);
self.scrollViewButtonViewLeft.frame = CGRectMake([self leftUtilityButtonsWidth], 0, [self leftUtilityButtonsWidth], _height);
self.scrollViewButtonViewRight.frame = CGRectMake(CGRectGetWidth(self.bounds), 0, [self rightUtilityButtonsWidth], _height);
self.scrollViewContentView.frame = CGRectMake([self leftUtilityButtonsWidth], 0, CGRectGetWidth(self.bounds), _height);
}
#pragma mark - Setup helpers
- (CGFloat)leftUtilityButtonsWidth {
return [_scrollViewButtonViewLeft utilityButtonsWidth];
}
- (CGFloat)rightUtilityButtonsWidth {
return [_scrollViewButtonViewRight utilityButtonsWidth];
}
- (CGFloat)utilityButtonsPadding {
return ([_scrollViewButtonViewLeft utilityButtonsWidth] + [_scrollViewButtonViewRight utilityButtonsWidth]);
}
- (CGPoint)scrollViewContentOffset {
return CGPointMake([_scrollViewButtonViewLeft utilityButtonsWidth], 0);
}
#pragma mark UIScrollView helpers
- (void)scrollToRight:(inout CGPoint *)targetContentOffset{
[self closeOtherDrawers];
targetContentOffset->x = [self utilityButtonsPadding];
_cellState = kCellStateRight;
if ([_delegate respondsToSelector:@selector(swippableTableViewCell:scrollingToState:)]) {
[_delegate swippableTableViewCell:self scrollingToState:kCellStateRight];
}
}
- (void)scrollToCenter:(inout CGPoint *)targetContentOffset {
[self closeOtherDrawers];
targetContentOffset->x = [self leftUtilityButtonsWidth];
_cellState = kCellStateCenter;
if ([_delegate respondsToSelector:@selector(swippableTableViewCell:scrollingToState:)]) {
[_delegate swippableTableViewCell:self scrollingToState:kCellStateCenter];
}
}
- (void)scrollToLeft:(inout CGPoint *)targetContentOffset{
[self closeOtherDrawers];
targetContentOffset->x = 0;
_cellState = kCellStateLeft;
if ([_delegate respondsToSelector:@selector(swippableTableViewCell:scrollingToState:)]) {
[_delegate swippableTableViewCell:self scrollingToState:kCellStateLeft];
}
}
#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 {
if (scrollView.contentOffset.x > [self leftUtilityButtonsWidth]) {
// Expose the right button view
self.scrollViewButtonViewRight.frame = CGRectMake(scrollView.contentOffset.x + (CGRectGetWidth(self.bounds) - [self rightUtilityButtonsWidth]), 0.0f, [self rightUtilityButtonsWidth], _height);
} else {
if (self.leftUtilityButtons.count == 0) {
scrollView.contentOffset = CGPointZero;
} else {
// Expose the left button view
self.scrollViewButtonViewLeft.frame = CGRectMake(scrollView.contentOffset.x, 0.0f, [self leftUtilityButtonsWidth], _height);
}
}
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == kTableViewCellObserverContext) {
if ([keyPath isEqualToString:@"contentOffset"]) {
if (_cellState != kCellStateCenter) {
[self hideUtilityButtonsAnimated:YES];
}
}
}
}
#pragma mark -
- (void)closeOtherDrawers {
if (self.containingTableView) {
for (UITableViewCell *cell in self.containingTableView.visibleCells) {
if ([cell isKindOfClass:[self class]]) {
SWTableViewCell *c = (SWTableViewCell *)cell;
if (c.cellState != kCellStateCenter) {
[c hideUtilityButtonsAnimated:YES];
}
}
}
}
}
@end
#pragma mark NSMutableArray class extension helper
@implementation NSMutableArray (SWUtilityButtons)
- (void)addUtilityButtonWithColor:(UIColor *)color title:(NSString *)title {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self addObject:button];
}
- (void)addUtilityButtonWithColor:(UIColor *)color icon:(UIImage *)icon {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setImage:icon forState:UIControlStateNormal];
[self addObject:button];
}
@end