-
Notifications
You must be signed in to change notification settings - Fork 0
/
OPTableView.m
executable file
·292 lines (233 loc) · 12.1 KB
/
OPTableView.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
// OPTableView
//
// Copyright (c) 2012 Brandon Williams (http://www.opetopic.com)
//
// 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 "OPTableView.h"
#import <objc/runtime.h>
@interface OPTableView () <UITableViewDelegate, UITableViewDataSource>{
BOOL snapAnimated;
}
@property (nonatomic, weak) id<UITableViewDataSource> realDataSource;
@property (nonatomic, weak) id<OPTableViewDelegate> realDelegate;
@property (nonatomic, assign) BOOL dataSourceIsSelf;
@property (nonatomic, assign) BOOL delegateIsSelf;
@property (nonatomic, strong) NSIndexPath *snappedIndexPath;
@property (nonatomic, assign) CGPoint contentOffsetDelta;
-(void) snapScrolling;
@end
@implementation OPTableView
@synthesize horizontal = _horizontal;
@synthesize snapToRows = _snapToRows;
@synthesize realDataSource = _realDataSource;
@synthesize realDelegate = _realDelegate;
@synthesize dataSourceIsSelf = _dataSourceIsSelf;
@synthesize delegateIsSelf = _delegateIsSelf;
@synthesize snappedIndexPath = _snappedIndexPath;
@synthesize contentOffsetDelta = _contentOffsetDelta;
@synthesize selectionDatasource = _selectionDatasource;
-(id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
if (! (self = [super initWithFrame:frame style:style]))
return nil;
self.snappedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
return self;
}
-(void) dealloc {
self.delegate = nil;
self.dataSource = nil;
}
-(void) setHorizontal:(BOOL)horizontal {
_horizontal = horizontal;
if (horizontal)
{
CGRect frame = self.frame;
self.transform = CGAffineTransformMakeRotation(-M_PI / 2.0f);
self.frame = frame;
}
else
{
CGRect frame = self.frame;
self.transform = CGAffineTransformIdentity;
self.frame = frame;
}
}
#pragma mark -
#pragma mark UITableView methods
#pragma mark -
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_realDataSource tableView:tableView numberOfRowsInSection:section];
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.realDataSource tableView:tableView cellForRowAtIndexPath:indexPath];
if (cell && self.horizontal)
cell.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
return cell;
}
#pragma mark -
#pragma mark UIScrollView methods
#pragma mark -
-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
static CGPoint previousOffset;
self.contentOffsetDelta = CGPointMake(self.contentOffset.x - previousOffset.x, self.contentOffset.y - previousOffset.y);
previousOffset = self.contentOffset;
// pass message to the delegate
if (! _delegateIsSelf && [self.realDelegate respondsToSelector:@selector(scrollViewDidScroll:)])
[self.realDelegate scrollViewDidScroll:scrollView];
}
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
snapAnimated=YES;
[self snapScrolling];
// when using UIScrollView paging also call the didSnapToIndexPath delegate method.
if (! self.snapToRows && self.pagingEnabled && [self.realDelegate respondsToSelector:@selector(tableView:didSnapToIndexPath:)])
[self.realDelegate tableView:self didSnapToIndexPath:[[self indexPathsForVisibleRows] lastObject]];
// pass message to the delegate
if (! _delegateIsSelf && [self.realDelegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)])
[self.realDelegate scrollViewDidEndDecelerating:scrollView];
}
-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
snapAnimated=YES;
[self snapScrolling];
// pass message to the delegate
if (! _delegateIsSelf && [self.realDelegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)])
[self.realDelegate scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
-(void) scrollViewDidScrollToTop:(UIScrollView *)scrollView {
self.snappedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
// pass message to the delegate
if (! _delegateIsSelf && [self.realDelegate respondsToSelector:@selector(scrollViewDidScrollToTop:)])
[self.realDelegate scrollViewDidScrollToTop:scrollView];
}
#pragma mark -
#pragma mark UITableView overridden methods
#pragma mark -
-(void) scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
[super scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
self.snappedIndexPath = indexPath;
}
#pragma mark -
#pragma mark NSObject overridden methods
#pragma mark -
- (BOOL)respondsToSelector:(SEL)aSelector {
if ([super respondsToSelector:aSelector])
return [super respondsToSelector:aSelector];
struct objc_method_description dataSourceMethod = protocol_getMethodDescription(@protocol(UITableViewDataSource), aSelector, NO, YES);
if (! _dataSourceIsSelf && dataSourceMethod.name != nil && [_realDataSource respondsToSelector:aSelector])
return YES;
struct objc_method_description delegateMethod = protocol_getMethodDescription(@protocol(UITableViewDelegate), aSelector, NO, YES);
if (! _delegateIsSelf && delegateMethod.name != nil && [_realDelegate respondsToSelector:aSelector])
return YES;
return NO;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
if ([super methodSignatureForSelector:aSelector])
return [super methodSignatureForSelector:aSelector];
struct objc_method_description dataSourceMethod = protocol_getMethodDescription(@protocol(UITableViewDataSource), aSelector, NO, YES);
if (dataSourceMethod.name != nil && [(NSObject*)_realDataSource methodSignatureForSelector:aSelector])
return [(NSObject*)_realDataSource methodSignatureForSelector:aSelector];
struct objc_method_description delegateMethod = protocol_getMethodDescription(@protocol(UITableViewDelegate), aSelector, NO, YES);
if (delegateMethod.name != nil && [(NSObject*)_realDelegate methodSignatureForSelector:aSelector])
return [(NSObject*)_realDelegate methodSignatureForSelector:aSelector];
return nil;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
SEL selector = [anInvocation selector];
struct objc_method_description dataSourceMethod = protocol_getMethodDescription(@protocol(UITableViewDataSource), selector, NO, YES);
struct objc_method_description delegateMethod = protocol_getMethodDescription(@protocol(UITableViewDelegate), selector, NO, YES);
if (dataSourceMethod.name != nil && [self.realDataSource respondsToSelector:selector])
[anInvocation invokeWithTarget:self.realDataSource];
else if (delegateMethod.name != nil && [self.realDelegate respondsToSelector:selector])
[anInvocation invokeWithTarget:self.realDelegate];
else
[super forwardInvocation:anInvocation];
}
#pragma mark -
#pragma mark Delegate / data source methods
#pragma mark -
-(id<OPTableViewDelegate>) delegate {
return self.realDelegate;
}
-(void) setDelegate:(id<OPTableViewDelegate>)delegate {
self.realDelegate = delegate;
self.delegateIsSelf = (id)delegate == (id)self;
[super setDelegate:self];
}
-(id<UITableViewDataSource>) dataSource {
return self.realDataSource;
}
-(void) setDataSource:(id<UITableViewDataSource>)dataSource {
self.realDataSource = dataSource;
self.dataSourceIsSelf = dataSource == self;
[super setDataSource:self];
}
-(void) setSelectionDatasource:(id<OPTableViewPreselectionDataSource>)selectionDatasource{
if(self.snapToRows){
NSIndexPath *preselectIndexPath=[selectionDatasource preSelectIndexPath];
float maxRowHeight=preselectIndexPath.row*self.rowHeight;
CGRect rectToScroll=CGRectMake(0, maxRowHeight, self.bounds.size.width, self.rowHeight);
dispatch_async(dispatch_get_main_queue(), ^{
[self scrollRectToVisible:rectToScroll animated:NO];
snapAnimated=NO;
//Calling snap without animation will snap to any imprefect forced scrolling.
[self snapScrolling];
});
}
}
#pragma mark -
#pragma mark Private methods
#pragma mark -
-(void) snapScrolling {
if (self.snapToRows)
{
// A little trick here. This method is usually called from a UIScrollView delegate method, which is taking place
// on the UI event tracking run loop mode. This means any UI animation stuff we try to do here may get skipped. But,
// if we dispatch async to the current queue we will pick up the NEXT default run loop, and so these animations
// will go through fine.
dispatch_async(dispatch_get_main_queue(), ^{
// allow for overflow elasticity in the scroll view
if (self.contentOffset.y < 0.0f)
return ;
else if (self.contentOffset.y > self.contentSize.height - self.frame.size.height)
return ;
// figure out which index path we should snap to next
UITableViewCell *firstCell = [[self visibleCells] objectAtIndex:0];
UITableViewCell *secondCell = [[self visibleCells] lastObject];
CGFloat top = firstCell.frame.origin.y - self.contentOffset.y;
CGFloat bottom = top + firstCell.frame.size.height;
if (self.contentOffsetDelta.y > 0 && ((top < -firstCell.frame.size.height/5.0f) || (self.contentOffsetDelta.y >= 10.0f)))
self.snappedIndexPath = [[self indexPathsForVisibleRows] lastObject];
else if (self.contentOffsetDelta.y < 0 && ((bottom > secondCell.frame.size.height/5.0f) || (self.contentOffsetDelta.y <= -10.0f)))
self.snappedIndexPath = [[self indexPathsForVisibleRows] objectAtIndex:0];
// allow the delegate to customize the snapped index path
if (self.snappedIndexPath && [self.realDelegate respondsToSelector:@selector(tableView:shouldSnapToIndexPath:)])
self.snappedIndexPath = [self.realDelegate tableView:self shouldSnapToIndexPath:self.snappedIndexPath];
// let our delegate know we are about to snap to the index path
if ([self.realDelegate respondsToSelector:@selector(tableView:willSnapToIndexPath:)])
[self.realDelegate tableView:self willSnapToIndexPath:self.snappedIndexPath];
[self scrollToRowAtIndexPath:self.snappedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:snapAnimated];
// a little hackish, but by waiting 0.3 seconds we can assume the above scroll animation finished,
// and so then we call the didSnap delegate method
if ([self.realDelegate respondsToSelector:@selector(tableView:didSnapToIndexPath:)]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
[self.realDelegate tableView:self didSnapToIndexPath:self.snappedIndexPath];
});
}
});
}
}
@end