-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathEKStreamView.m
483 lines (358 loc) · 14.8 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
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
//
// 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;
@synthesize headerView, footerView, contentView;
- (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)layoutSubviews {
[super layoutSubviews];
if (infoForCells.count) {
NSInteger numberOfColumns = [delegate numberOfColumnsInStreamView:self];
CGFloat destWidth = (self.bounds.size.width - (numberOfColumns + 1) * self.columnPadding) / numberOfColumns;
if (ABS(destWidth - columnWidth) < 0.01f) {
return;
}
}
[self reloadData];
}
- (void)reloadData
{
[cellHeightsByIndex removeAllObjects];
[cellHeightsByColumn removeAllObjects];
[rectsForCells removeAllObjects];
[infoForCells removeAllObjects];
[cellCache removeAllObjects];
for (EKStreamViewCellInfo *cellInfo in visibleCellInfo) {
[cellInfo.cell removeFromSuperview];
}
//We could not simply remove headerView/footerView from their parentView:we need to persist their state.
//For example, if an UITextView is a subview of headerView/footerView, remove them from parentView will make the keyboard hide.
if ([delegate respondsToSelector:@selector(headerForStreamView:)]) {
UIView *tempHeaderView = [delegate headerForStreamView:self];
if (tempHeaderView) {
CGRect f = tempHeaderView.frame;
f.origin = CGPointMake(0, cellPadding);
f.size.width = self.bounds.size.width - columnPadding * 2;
tempHeaderView.frame = f;
if (headerView != tempHeaderView) {
[headerView removeFromSuperview];
headerView = tempHeaderView;
[contentView addSubview:headerView];
}
}
else {
[headerView removeFromSuperview];
headerView = nil;
}
} else {
[headerView removeFromSuperview];
headerView = nil;
}
if ([delegate respondsToSelector:@selector(footerForStreamView:)]) {
UIView *tempFooterView = [delegate footerForStreamView:self];
if (tempFooterView) {
if (footerView != tempFooterView) {
footerView = tempFooterView;
[contentView addSubview:footerView];
}
}
else {
[footerView removeFromSuperview];
footerView = nil;
}
} else {
[footerView removeFromSuperview];
footerView = nil;
}
// calculate height for all cells
NSInteger numberOfColumns = [delegate numberOfColumnsInStreamView:self];
columnWidth = (self.bounds.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(numberOfColumns, sizeof(CGFloat));
if (columnHeights == NULL || cellX == NULL) {
[NSException raise:NSMallocException format:@"Allocating memory failed."];
}
CGFloat cellHeight = headerView ? headerView.bounds.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:@(height)];
NSUInteger shortestCol = 0;
for (int j = 1; j < numberOfColumns; j++) {
if (columnHeights[j] < columnHeights[shortestCol] - 0.5f)
shortestCol = j;
}
NSMutableArray *cellHeightsInCol = cellHeightsByColumn[shortestCol];
[cellHeightsInCol addObject:@(height)];
NSMutableArray *rectsForCellsInCol = rectsForCells[shortestCol];
EKStreamViewCellInfo *info = [EKStreamViewCellInfo new];
info.frame = CGRectMake(cellX[shortestCol], columnHeights[shortestCol] + cellPadding, columnWidth, height);
info.index = i;
[rectsForCellsInCol addObject:info];
[infoForCells 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);
f.size.width = self.bounds.size.width - columnPadding * 2;
footerView.frame = f;
maxHeight += footerView.bounds.size.height + cellPadding;
}
self.contentSize = CGSizeMake(0.0f, maxHeight);
CGRect f = contentView.frame;
f.size.height = maxHeight;
f.size.width = self.bounds.size.width;
contentView.frame = f;
free(columnHeights);
free(cellX);
}
- (id<EKResusableCell>)dequeueReusableCellWithIdentifier:(NSString *)identifier
{
NSMutableArray *cellArray = cellCache[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.bounds.size.width - (numColumns + 1) * self.columnPadding) / numColumns;
}
#pragma mark - Private Methods
- (NSSet *)getVisibleCellInfo
{
CGFloat offsetTop = self.contentOffset.y;
CGFloat offsetBottom = offsetTop + self.bounds.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[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;
if ([delegate respondsToSelector:@selector(streamView:willDisplayCell:forIndex:)]) {
[delegate streamView:self willDisplayCell:cell forIndex:info.index];
}
[contentView 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];
infoForCells = [[NSMutableArray alloc] initWithCapacity:30];
contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.contentSize.width, self.contentSize.height)];
contentView.autoresizesSubviews = NO;
[self addSubview:contentView];
}
- (void)scrollToCellAtIndex:(NSUInteger)index atScrollPosition:(EKStreamViewScrollPosition)scrollPosition animated:(BOOL)animated
{
if (scrollPosition == EKStreamViewScrollPositionNone) {
return;
}
EKStreamViewCellInfo *cellInfo = infoForCells[index];
CGFloat cellPositionY = cellInfo.frame.origin.y;
CGFloat cellHeight = cellInfo.frame.size.height;
CGFloat viewHeight = self.frame.size.height;
CGFloat targetOffsetY = self.contentOffset.y;
CGFloat minOffsetY = 0.0f;
CGFloat maxOffsetY = self.contentSize.height - self.frame.size.height;
switch (scrollPosition) {
case EKStreamViewScrollPositionNone:
break;
case EKStreamViewScrollPositionTop:
targetOffsetY = MIN(maxOffsetY, MAX(minOffsetY, cellPositionY));
break;
case EKStreamViewScrollPositionMiddle:
targetOffsetY = MIN(maxOffsetY, MAX(minOffsetY, cellPositionY - (viewHeight - cellHeight) * 0.5));
break;
case EKStreamViewScrollPositionBottom:
targetOffsetY = MIN(maxOffsetY, MAX(minOffsetY, cellPositionY - (viewHeight - cellHeight)));
break;
}
[self setContentOffset:CGPointMake(0.0f, targetOffsetY) animated:animated];
}
@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[cellID];
if (cellArray == nil) {
cellArray = [NSMutableArray arrayWithCapacity:10];
cellCache[cellID] = cellArray;
}
[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