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
385 lines (304 loc) · 14.4 KB
/
SWTableViewCell.m
File metadata and controls
385 lines (304 loc) · 14.4 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
//
// 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 NSString * const kTableViewCellContentView = @"UITableViewCellContentView";
typedef enum {
kCellStateCenter,
kCellStateLeft,
kCellStateRight
} SWCellState;
#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; // eh.
}
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; // eh.
}
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:self.parentCell action:self.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;
@property (nonatomic, weak) UITableView *containingTableView; // Used for row height and selection
@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 initializer];
}
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)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) {
// Actually select the row
if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]){
NSIndexPath *cellIndexPath = [_containingTableView indexPathForCell:self];
[self.containingTableView.delegate tableView:_containingTableView didSelectRowAtIndexPath:cellIndexPath];
}
} else {
// Scroll back to center
[self hideUtilityButtonsAnimated:YES];
}
}
#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;
}
#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{
targetContentOffset->x = [self utilityButtonsPadding];
_cellState = kCellStateRight;
}
- (void)scrollToCenter:(inout CGPoint *)targetContentOffset {
targetContentOffset->x = [self leftUtilityButtonsWidth];
_cellState = kCellStateCenter;
}
- (void)scrollToLeft:(inout CGPoint *)targetContentOffset{
targetContentOffset->x = 0;
_cellState = 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 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 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 {
// Expose the left button view
self.scrollViewButtonViewLeft.frame = CGRectMake(scrollView.contentOffset.x, 0.0f, [self leftUtilityButtonsWidth], _height);
}
}
@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