/
XJWaterfallView.m
472 lines (349 loc) · 14.8 KB
/
XJWaterfallView.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
//
// XJWaterfallView.m
// XJWaterfallView
//
// Created by Xiantao Jiao on 4/23/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "XJPetalView.h"
#import "XJPetalViewInfo.h"
#import "XJWaterfallPathInfo.h"
#import "XJWaterfallView.h"
@interface XJWaterfallView() {
@private
__unsafe_unretained id<XJWaterfallViewDataSource> dataSource_;
UIView* backgroundView_;
CGFloat petalViewGap_;
CGFloat rightMargin_;
NSArray* visiblePetalViews_;
NSMutableDictionary* reusablePetalViews_;
NSUInteger numberOfPaths_;
NSArray* pathInfos_;
}
@property (nonatomic, strong) NSArray* visiblePetalViews;
@property (nonatomic, strong) NSMutableDictionary* reusablePetalViews;
@property (nonatomic, assign) NSUInteger numberOfPaths;
@property (nonatomic, strong) NSArray* pathInfos;
+ (void) reverseArray:(NSMutableArray*)array;
- (XJWaterfallPathInfo*) infoOfShortestPath;
- (XJWaterfallPathInfo*) infoOfHighestPath;
- (void) prepareParametersNeededForLayout;
- (void) resetContentSizeByAppendingPetalViews;
- (void) removeAllVisiblePetalViews;
- (void) tilePetalViewsOnPath:(XJWaterfallPathInfo*)pathInfo minimumY:(CGFloat)minY maximumY:(CGFloat)maxY;
- (void) tilePetalViewWithInfo:(XJPetalViewInfo*)petalViewInfo petalViewList:(NSMutableArray*)petalViews;
- (void) reloadPetalViewsOnPath:(XJWaterfallPathInfo*)pathInfo petalViewList:(NSMutableArray*)petalViews
minimumY:(CGFloat)minY maximumY:(CGFloat)maxY;
- (NSInteger) firstRowBelowY:(CGFloat)y inPath:(XJWaterfallPathInfo*)pathInfo;
- (void) pushPetalViewForReuse:(XJPetalView*)petalView;
- (XJPetalView*) popReusablePetalViewWithIdentifier:(NSString*)identifier;
@end
@implementation XJWaterfallView
#pragma mark - Private static members
const static NSUInteger DEFAULT_NUMBER_OF_PATHS = 3;
const static CGFloat DEFAULT_PETAL_VIEW_GAP = 5.0f;
const static CGFloat DEFAULT_RIGHT_MARGIN = 5.0f;
static NSString* PETAL_VIEW_ROW_KEY = @"__PETAL_VIEW_ROW__";
#pragma mark - Initializers and uninitializers
- (id) initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame]) != nil) {
[self setBackgroundColor:[UIColor whiteColor]];
[self setPetalViewGap:DEFAULT_PETAL_VIEW_GAP];
[self setRightMargin:DEFAULT_RIGHT_MARGIN];
}
return self;
}
#pragma mark - Public static methods
+ (void) reverseArray:(NSMutableArray*)array {
for (NSInteger i = 0, j = [array count] - 1; i < j; ++i, --j) {
id tmp = [array objectAtIndex:i];
[array replaceObjectAtIndex:i withObject:[array objectAtIndex:j]];
[array replaceObjectAtIndex:j withObject:tmp];
}
}
#pragma mark - Public methods
@synthesize dataSource = dataSource_;
@synthesize backgroundView = backgroundView_;
- (void) setBackgroundView:(UIView*)backgroundView {
if (backgroundView != backgroundView_) {
if ([backgroundView_ superview] == self) {
[backgroundView_ removeFromSuperview];
}
backgroundView_ = backgroundView;
[backgroundView_ setFrame:[self bounds]];
[self addSubview:backgroundView_];
}
}
@synthesize petalViewGap = petalViewGap_;
@synthesize rightMargin = rightMargin_;
- (XJPetalView*) dequeueReusablePetalViewWithIdentifier:(NSString*)identifier {
XJPetalView* petalView = [self popReusablePetalViewWithIdentifier:identifier];
[petalView prepareForReuse];
return petalView;
}
- (void) reloadData {
// Removes old petal views.
[self removeAllVisiblePetalViews];
[self setReusablePetalViews:nil];
// Resets path count, path widths and heights, as well as content size.
[self prepareParametersNeededForLayout];
[self resetContentSizeByAppendingPetalViews];
// Scrolls to top and tiles new petal views.
[self scrollsToTop];
[self setNeedsLayout];
}
- (void) appendPetals {
[self resetContentSizeByAppendingPetalViews];
[self setNeedsLayout];
}
#pragma mark - UIView methods
- (void) layoutSubviews {
[super layoutSubviews];
CGRect bounds = [self bounds];
CGFloat minY = CGRectGetMinY(bounds);
CGFloat maxY = CGRectGetMaxY(bounds);
[backgroundView_ setFrame:bounds];
[[self pathInfos] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
[self tilePetalViewsOnPath:((XJWaterfallPathInfo*) obj) minimumY:minY maximumY:maxY];
}];
// Always enables scrolling.
CGSize contentSize = [self contentSize];
if (contentSize.height <= [self bounds].size.height) {
contentSize.height = [self bounds].size.height + 1.0f;
[self setContentSize:contentSize];
}
}
#pragma mark - Private methods
@synthesize visiblePetalViews = visiblePetalViews_;
@synthesize reusablePetalViews = reusablePetalViews_;
- (NSMutableDictionary*) reusablePetalViews {
if (reusablePetalViews_ == nil) {
[self setReusablePetalViews:[NSMutableDictionary dictionaryWithCapacity:0]];
}
return reusablePetalViews_;
}
@synthesize numberOfPaths = numberOfPaths_;
@synthesize pathInfos = pathInfos_;
- (XJWaterfallPathInfo*) infoOfShortestPath {
__block XJWaterfallPathInfo* bestPathInfo = nil;
__block CGFloat minValue = 0.0f;
[[self pathInfos] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
XJWaterfallPathInfo* pathInfo = (XJWaterfallPathInfo*) obj;
if (bestPathInfo == nil || [pathInfo height] < minValue) {
bestPathInfo = pathInfo;
minValue = [pathInfo height];
}
}];
return bestPathInfo;
}
- (XJWaterfallPathInfo*) infoOfHighestPath {
__block XJWaterfallPathInfo* bestPathInfo = nil;
__block CGFloat maxValue = 0.0f;
[[self pathInfos] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
XJWaterfallPathInfo* pathInfo = (XJWaterfallPathInfo*) obj;
if (bestPathInfo == nil || [pathInfo height] > maxValue) {
bestPathInfo = pathInfo;
maxValue = [pathInfo height];
}
}];
return bestPathInfo;
}
- (void) prepareParametersNeededForLayout {
if ([[self dataSource] respondsToSelector:@selector(numberOfPathsForWaterfallView:)] == YES) {
[self setNumberOfPaths:[[self dataSource] numberOfPathsForWaterfallView:self]];
} else {
[self setNumberOfPaths:DEFAULT_NUMBER_OF_PATHS];
}
[self setPathInfos:nil];
if ([self numberOfPaths] == 0) {
return;
}
CGFloat spaceWidth = [self numberOfPaths] * [self petalViewGap] + [self rightMargin];
CGFloat fixedPathWidth = -1.0f;
if ([[self dataSource] respondsToSelector:@selector(waterfallView:widthOfPathOnColumn:)] == NO) {
fixedPathWidth = ([self bounds].size.width - spaceWidth) / [self numberOfPaths];
if (fixedPathWidth < 0.0f) {
fixedPathWidth = 0.0f;
}
}
CGFloat pathStartX = [self petalViewGap];
NSMutableArray* visiblePetalViews = [NSMutableArray arrayWithCapacity:[self numberOfPaths]];
NSMutableArray* pathInfos = [NSMutableArray arrayWithCapacity:[self numberOfPaths]];
for (NSUInteger col = 0; col < [self numberOfPaths]; ++col) {
[visiblePetalViews addObject:[NSMutableArray arrayWithCapacity:0]];
CGFloat pathWidth = 0.0f;
if (fixedPathWidth < 0.0f) {
pathWidth = [[self dataSource] waterfallView:self widthOfPathOnColumn:col];
} else {
pathWidth = fixedPathWidth;
}
XJWaterfallPathInfo* pathInfo = [[XJWaterfallPathInfo alloc] init];
[pathInfo setColumn:col];
[pathInfo setX:pathStartX];
[pathInfo setWidth:pathWidth];
[pathInfos addObject:pathInfo];
pathStartX += pathWidth + [self petalViewGap];
}
[self setVisiblePetalViews:visiblePetalViews];
[self setPathInfos:pathInfos];
}
- (void) resetContentSizeByAppendingPetalViews {
if ([self numberOfPaths] == 0) {
[self setContentSize:[self bounds].size];
return;
}
CGFloat spaceWidth = [self numberOfPaths] * [self petalViewGap] + [self rightMargin];
__block CGRect contentFrame = CGRectMake(0.0f, 0.0f, spaceWidth, 0.0f);
__block NSUInteger fromIndex = 0;
// Calculates content width.
[[self pathInfos] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
XJWaterfallPathInfo* pathInfo = (XJWaterfallPathInfo*) obj;
contentFrame.size.width += [pathInfo width];
fromIndex += [pathInfo numberOfPetals];
}];
// Calculates content height, at the same time calculates frame of each petal view.
NSUInteger numberOfPetal = [[self dataSource] numberOfPetalsForWaterfallView:self];
for (NSUInteger index = fromIndex; index < numberOfPetal; ++index) {
XJWaterfallPathInfo* pathInfo = [self infoOfShortestPath];
CGFloat x = [pathInfo x];
CGFloat y = [pathInfo height] + [self petalViewGap];
CGFloat width = [pathInfo width];
CGFloat normalizedHeight = [[self dataSource] waterfallView:self normalizedHeightOfPetalViewAtIndex:index];
CGFloat height = normalizedHeight * width;
XJPetalViewInfo* petalViewInfo = [[XJPetalViewInfo alloc] init];
[petalViewInfo setIndex:index];
[petalViewInfo setRow:[pathInfo numberOfPetals]];
[petalViewInfo setFrame:CGRectMake(x, y, width, height)];
[pathInfo addPetalViewInfo:petalViewInfo];
}
contentFrame.size.height = [[self infoOfHighestPath] height] + [self petalViewGap];
// Resets content size.
contentFrame = CGRectIntegral(contentFrame);
[self setContentSize:contentFrame.size];
}
- (void) removeAllVisiblePetalViews {
[[self visiblePetalViews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
[(NSArray*) obj enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
[(XJPetalView*) obj removeFromSuperview];
}];
}];
[self setVisiblePetalViews:nil];
}
- (void) tilePetalViewsOnPath:(XJWaterfallPathInfo*)pathInfo minimumY:(CGFloat)minY maximumY:(CGFloat)maxY {
NSMutableArray* petalViews = [[self visiblePetalViews] objectAtIndex:[pathInfo column]];
// Recycles invisible petal views.
[[self class] reverseArray:petalViews];
while ([petalViews count] > 0) {
XJPetalView* petalView = [petalViews lastObject];
NSInteger row = [[[petalView layer] valueForKey:PETAL_VIEW_ROW_KEY] integerValue];
CGRect petalViewFrame = [[pathInfo petalViewInfoForRow:row] frame];
if (CGRectGetMaxY(petalViewFrame) > minY) {
break;
}
[petalView removeFromSuperview];
[petalViews removeLastObject];
[self pushPetalViewForReuse:petalView];
}
[[self class] reverseArray:petalViews];
while ([petalViews count] > 0) {
XJPetalView* petalView = [petalViews lastObject];
NSInteger row = [[[petalView layer] valueForKey:PETAL_VIEW_ROW_KEY] integerValue];
CGRect petalViewFrame = [[pathInfo petalViewInfoForRow:row] frame];
if (CGRectGetMinY(petalViewFrame) < maxY) {
break;
}
[petalView removeFromSuperview];
[petalViews removeLastObject];
[self pushPetalViewForReuse:petalView];
}
// Tiles visible petal views.
if ([petalViews count] == 0) {
[self reloadPetalViewsOnPath:pathInfo petalViewList:petalViews minimumY:minY maximumY:maxY];
} else {
[[self class] reverseArray:petalViews];
NSInteger firstRow = [[[[petalViews lastObject] layer] valueForKey:PETAL_VIEW_ROW_KEY] integerValue];
for (firstRow--; firstRow >= 0; --firstRow) {
XJPetalViewInfo* petalViewInfo = [pathInfo petalViewInfoForRow:firstRow];
CGRect petalViewFrame = [[pathInfo petalViewInfoForRow:firstRow] frame];
if (CGRectGetMaxY(petalViewFrame) <= minY) {
break;
}
[self tilePetalViewWithInfo:petalViewInfo petalViewList:petalViews];
}
[[self class] reverseArray:petalViews];
NSInteger lastRow = [[[[petalViews lastObject] layer] valueForKey:PETAL_VIEW_ROW_KEY] integerValue];
for (lastRow++; lastRow < [pathInfo numberOfPetals]; ++lastRow) {
XJPetalViewInfo* petalViewInfo = [pathInfo petalViewInfoForRow:lastRow];
CGRect petalViewFrame = [petalViewInfo frame];
if (CGRectGetMinY(petalViewFrame) >= maxY) {
break;
}
[self tilePetalViewWithInfo:petalViewInfo petalViewList:petalViews];
}
}
}
- (void) tilePetalViewWithInfo:(XJPetalViewInfo*)petalViewInfo petalViewList:(NSMutableArray*)petalViews {
XJPetalView* petalView = [[self dataSource] waterfallView:self petalViewAtIndex:[petalViewInfo index]];
[[petalView layer] setValue:[NSNumber numberWithInteger:[petalViewInfo row]] forKey:PETAL_VIEW_ROW_KEY];
[petalView setFrame:CGRectIntegral([petalViewInfo frame])];
[petalViews addObject:petalView];
NSInteger count = [[self subviews] count];
if (count == 0) {
[self addSubview:petalView];
} else {
[self insertSubview:petalView atIndex:(count - 1)];
}
}
- (void) reloadPetalViewsOnPath:(XJWaterfallPathInfo*)pathInfo petalViewList:(NSMutableArray*)petalViews
minimumY:(CGFloat)minY maximumY:(CGFloat)maxY {
NSInteger row = [self firstRowBelowY:minY inPath:pathInfo];
for (; row < [pathInfo numberOfPetals]; ++row) {
XJPetalViewInfo* petalViewInfo = [pathInfo petalViewInfoForRow:row];
CGRect petalViewFrame = [petalViewInfo frame];
if (CGRectGetMinY(petalViewFrame) >= maxY) {
break;
}
[self tilePetalViewWithInfo:petalViewInfo petalViewList:petalViews];
}
}
- (NSInteger) firstRowBelowY:(CGFloat)y inPath:(XJWaterfallPathInfo*)pathInfo {
NSInteger row = [pathInfo numberOfPetals];
NSInteger left = 0;
NSInteger right = [pathInfo numberOfPetals] - 1;
while (left <= right) {
NSInteger mid = (left + right) / 2;
if (CGRectGetMaxY([[pathInfo petalViewInfoForRow:mid] frame]) < y) {
left = mid + 1;
} else {
right = mid - 1;
row = MIN(row, mid);
}
}
return row;
}
- (void) pushPetalViewForReuse:(XJPetalView*)petalView {
if ([petalView reuseIdentifier] == nil) {
return;
}
NSMutableArray* petalViews = [[self reusablePetalViews] objectForKey:[petalView reuseIdentifier]];
if (petalViews == nil) {
petalViews = [NSMutableArray arrayWithCapacity:0];
[[self reusablePetalViews] setObject:petalViews forKey:[petalView reuseIdentifier]];
}
[petalViews addObject:petalView];
}
- (XJPetalView*) popReusablePetalViewWithIdentifier:(NSString*)identifier {
if (identifier == nil) {
return nil;
}
NSMutableArray* petalViews = [[self reusablePetalViews] objectForKey:identifier];
if ([petalViews count] == 0) {
return nil;
} else {
XJPetalView* petalView = [petalViews lastObject];
[petalViews removeLastObject];
return petalView;
}
}
@end