forked from ekohe/EKStreamView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEKStreamView.m
396 lines (283 loc) · 11.6 KB
/
EKStreamView.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
//
// EKStreamView.m
// StreamView
//
// Created by Eli Wang on 1/16/12.
// Copyright (c) 2012 ekohe.com. All rights reserved.
//
#import "EKStreamView.h"
@implementation EKStreamViewCellInfo
@synthesize frame, index, cell;
- (BOOL)isEqual:(id)object
{
if (![object isKindOfClass:[EKStreamViewCellInfo class]]) return NO;
return index == [object index];
}
- (NSUInteger)hash
{
return index;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: index: %d>",NSStringFromClass([self class]), index];
}
@end
@interface EKStreamView()
- (void)setup;
- (NSSet *)getVisibleCellInfo;
- (void)layoutCellWithCellInfo:(EKStreamViewCellInfo *)info;
@property (nonatomic) NSSet *visibleCellInfo;
@property (nonatomic) NSMutableDictionary *cellCache;
@end
@implementation EKStreamView
@synthesize delegate, visibleCellInfo, cellCache, cellPadding, columnPadding;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setup];
}
return self;
}
- (void)reloadData
{
[cellHeightsByIndex removeAllObjects];
[cellHeightsByColumn removeAllObjects];
[rectsForCells removeAllObjects];
[cellCache removeAllObjects];
[headerView removeFromSuperview];
[footerView removeFromSuperview];
for (EKStreamViewCellInfo *cellInfo in visibleCellInfo) {
[cellInfo.cell removeFromSuperview];
}
if ([delegate respondsToSelector:@selector(headerForStreamView:)]) {
headerView = [delegate headerForStreamView:self];
CGRect f = headerView.frame;
f.origin = CGPointMake(columnPadding, cellPadding);
headerView.frame = f;
[self addSubview:headerView];
} else {
headerView = nil;
}
if ([delegate respondsToSelector:@selector(footerForStreamView:)]) {
footerView = [delegate footerForStreamView:self];
[self addSubview:footerView];
} else {
footerView = nil;
}
// calculate height for all cells
NSInteger numberOfColumns = [delegate numberOfColumnsInStreamView:self];
columnWidth = (self.frame.size.width - (numberOfColumns + 1) * self.columnPadding) / numberOfColumns;
if (numberOfColumns < 1)
[NSException raise:NSInvalidArgumentException format:@"The number of columns must be equal or greater than 1!"];
NSInteger numberOfCells = [delegate numberOfCellsInStreamView:self];
CGFloat *columnHeights = calloc(numberOfColumns, sizeof(CGFloat));
CGFloat *cellX = calloc(numberOfCells, sizeof(CGFloat));
if (columnHeights == NULL || cellX == NULL) {
[NSException raise:NSMallocException format:@"Allocating memory failed."];
}
CGFloat cellHeight = headerView ? headerView.frame.size.height + cellPadding : cellPadding;
for (int i = 0; i < numberOfColumns; i++) {
[cellHeightsByColumn addObject:[NSMutableArray arrayWithCapacity:20]];
[rectsForCells addObject:[NSMutableArray arrayWithCapacity:20]];
cellX[i] = (i == 0 ? columnPadding : cellX[i - 1] + columnWidth + columnPadding);
columnHeights[i] = cellHeight;
}
for (int i = 0; i < numberOfCells; i++) {
CGFloat height = [delegate streamView:self heightForCellAtIndex:i];
[cellHeightsByIndex addObject:[NSNumber numberWithFloat:height]];
NSUInteger shortestCol = 0;
for (int j = 1; j < numberOfColumns; j++) {
if (columnHeights[j] < columnHeights[shortestCol])
shortestCol = j;
}
NSMutableArray *cellHeightsInCol = [cellHeightsByColumn objectAtIndex:shortestCol];
[cellHeightsInCol addObject:[NSNumber numberWithFloat:height]];
NSMutableArray *rectsForCellsInCol = [rectsForCells objectAtIndex:shortestCol];
EKStreamViewCellInfo *info = [EKStreamViewCellInfo new];
info.frame = CGRectMake(cellX[shortestCol], columnHeights[shortestCol] + cellPadding, columnWidth, height);
info.index = i;
[rectsForCellsInCol addObject:info];
columnHeights[shortestCol] += height + cellPadding;
}
// determine the visible cells' range
visibleCellInfo = [self getVisibleCellInfo];
// draw the visible cells
for (EKStreamViewCellInfo *info in visibleCellInfo) {
[self layoutCellWithCellInfo:info];
}
CGFloat maxHeight = 0;
for (int i = 0; i < numberOfColumns; i++) {
if (columnHeights[i] > maxHeight)
maxHeight = columnHeights[i];
}
maxHeight += cellPadding;
if (footerView) {
CGRect f = footerView.frame;
f.origin = CGPointMake(columnPadding, maxHeight);
footerView.frame = f;
maxHeight += footerView.frame.size.height + cellPadding;
}
self.contentSize = CGSizeMake(0.0f, maxHeight);
free(columnHeights);
free(cellX);
}
- (id<EKResusableCell>)dequeueReusableCellWithIdentifier:(NSString *)identifier
{
NSMutableArray *cellArray = [cellCache objectForKey:identifier];
id<EKResusableCell> cell = nil;
if ([cellArray count] > 0) {
cell = [cellArray lastObject];
[cellArray removeLastObject];
}
return cell;
}
- (CGFloat)columnWidth
{
NSInteger numColumns = [delegate numberOfColumnsInStreamView:self];
return (self.frame.size.width - (numColumns + 1) * self.columnPadding) / numColumns;
}
#pragma mark - Private Methods
- (NSSet *)getVisibleCellInfo
{
CGFloat offsetTop = self.contentOffset.y;
CGFloat offsetBottom = offsetTop + self.frame.size.height;
NSMutableSet *ret = [NSMutableSet setWithCapacity:10];
for (NSMutableArray *rectsForCellsInCol in rectsForCells) {
for (int i = 0, c = [rectsForCellsInCol count]; i < c; i++) {
EKStreamViewCellInfo *info = [rectsForCellsInCol objectAtIndex:i];
CGFloat top = info.frame.origin.y;
CGFloat bottom = CGRectGetMaxY(info.frame);
if (bottom < offsetTop) { // The cell is above the current view rect
continue;
} else if (top > offsetBottom) { // the cell is below the current view rect. stop searching this column
break;
} else {
[ret addObject:info];
}
}
}
return ret;
}
- (void)layoutCellWithCellInfo:(EKStreamViewCellInfo *)info
{
UIView<EKResusableCell> *cell = [delegate streamView:self cellAtIndex:info.index];
cell.frame = info.frame;
info.cell = cell;
[self addSubview:cell];
}
- (void)setup
{
delegateObj = [EKStreamViewUIScrollViewDelegate new];
delegateObj.streamView = self;
[super setDelegate:delegateObj];
cellHeightsByIndex = [[NSMutableArray alloc] initWithCapacity:30];
cellHeightsByColumn = [[NSMutableArray alloc] initWithCapacity:5];
rectsForCells = [[NSMutableArray alloc] initWithCapacity:5];
cellCache = [[NSMutableDictionary alloc] initWithCapacity:20];
}
@end
@implementation EKStreamViewUIScrollViewDelegate
@synthesize streamView;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSSet *newVisibleCellInfo = [streamView getVisibleCellInfo];
NSSet *visibleCellInfo = streamView.visibleCellInfo;
NSMutableDictionary *cellCache = streamView.cellCache;
for (EKStreamViewCellInfo *info in visibleCellInfo) {
if (![newVisibleCellInfo containsObject:info]) {
// info.cell.retainCount: 1
NSString *cellID = info.cell.reuseIdentifier;
NSMutableArray *cellArray = [cellCache objectForKey:cellID];
if (cellArray == nil) {
cellArray = [NSMutableArray arrayWithCapacity:10];
[cellCache setObject:cellArray forKey:cellID];
}
[cellArray addObject:info.cell];
// info.cell.retainCount: 2
[info.cell removeFromSuperview];
// info.cell.retainCount: 1
}
}
for (EKStreamViewCellInfo *info in newVisibleCellInfo) {
if (![visibleCellInfo containsObject:info]) {
[streamView layoutCellWithCellInfo:info];
}
}
streamView.visibleCellInfo = newVisibleCellInfo;
if ([streamView.delegate respondsToSelector:@selector(scrollViewDidScroll:)])
[streamView.delegate scrollViewDidScroll:streamView];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewDidZoom:)])
[streamView.delegate scrollViewDidZoom:streamView];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewWillBeginDragging:)])
[streamView.delegate scrollViewWillBeginDragging:streamView];
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)])
[streamView.delegate scrollViewWillEndDragging:streamView withVelocity:velocity targetContentOffset:targetContentOffset];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)])
[streamView.delegate scrollViewDidEndDragging:streamView willDecelerate:decelerate];
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewWillBeginDecelerating:)])
[streamView.delegate scrollViewWillBeginDecelerating:streamView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)])
[streamView.delegate scrollViewDidEndDecelerating:streamView];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewDidEndScrollingAnimation:)])
[streamView.delegate scrollViewDidEndScrollingAnimation:streamView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
if ([streamView.delegate respondsToSelector:@selector(viewForZoomingInScrollView:)])
return [streamView.delegate viewForZoomingInScrollView:streamView];
else
return nil;
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewWillBeginZooming:withView:)])
[streamView.delegate scrollViewWillBeginZooming:scrollView withView:view];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewDidEndZooming:withView:atScale:)])
[streamView.delegate scrollViewDidEndZooming:streamView withView:view atScale:scale];
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewShouldScrollToTop:)])
return [streamView.delegate scrollViewShouldScrollToTop:streamView];
else
return YES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
if ([streamView.delegate respondsToSelector:@selector(scrollViewDidScrollToTop:)])
[streamView.delegate scrollViewDidScrollToTop:streamView];
}
@end