Repository navigation
Expand file tree
/
Copy pathZLCollectionViewVerticalLayout.m
More file actions
executable file
·707 lines (680 loc) · 46 KB
/
Copy pathZLCollectionViewVerticalLayout.m
File metadata and controls
executable file
·707 lines (680 loc) · 46 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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//
// ZLCollectionViewVerticalLayout.m
// ZLCollectionView
//
// Created by zhaoliang chen on 2019/1/25.
// Copyright © 2019 zhaoliang chen. All rights reserved.
//
#import "ZLCollectionViewVerticalLayout.h"
#import "ZLCollectionReusableView.h"
#import "ZLCollectionViewLayoutAttributes.h"
#import "ZLCollectionViewBackgroundViewLayoutAttributes.h"
@interface ZLCollectionViewVerticalLayout ()
@end
@implementation ZLCollectionViewVerticalLayout
#pragma mark - 初始化属性
- (instancetype)init {
self = [super init];
if (self) {
self.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return self;
}
- (void)prepareLayout {
[super prepareLayout];
//这里很关键,不加此判断在悬浮情况下将卡的怀疑人生...
if (!self.isNeedReCalculateAllLayout) {
return;
}
CGFloat totalWidth = self.collectionView.frame.size.width;
CGFloat x = 0;
CGFloat y = 0;
CGFloat headerH = 0;
CGFloat footerH = 0;
UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
CGFloat minimumLineSpacing = 0;
CGFloat minimumInteritemSpacing = 0;
NSUInteger sectionCount = [self.collectionView numberOfSections];
self.attributesArray = [NSMutableArray new];
[self.headerAttributesArray removeAllObjects];
self.collectionHeightsArray = [NSMutableArray arrayWithCapacity:sectionCount];
for (int index= 0; index<sectionCount; index++) {
NSUInteger itemCount = [self.collectionView numberOfItemsInSection:index];
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForHeaderInSection:)]) {
headerH = [self.delegate collectionView:self.collectionView layout:self referenceSizeForHeaderInSection:index].height;
} else {
headerH = self.headerReferenceSize.height;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForFooterInSection:)]) {
footerH = [self.delegate collectionView:self.collectionView layout:self referenceSizeForFooterInSection:index].height;
} else {
footerH = self.footerReferenceSize.height;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
edgeInsets = [self.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:index];
} else {
edgeInsets = self.sectionInset;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:minimumLineSpacingForSectionAtIndex:)]) {
minimumLineSpacing = [self.delegate collectionView:self.collectionView layout:self minimumLineSpacingForSectionAtIndex:index];
} else {
minimumLineSpacing = self.minimumLineSpacing;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
minimumInteritemSpacing = [self.delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:index];
} else {
minimumInteritemSpacing = self.minimumInteritemSpacing;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:registerBackView:)]) {
NSString* className = [self.delegate collectionView:self.collectionView layout:self registerBackView:index];
if (className != nil && className.length > 0) {
NSAssert([[NSClassFromString(className) alloc]init]!=nil, @"代理collectionView:layout:registerBackView:里面必须返回有效的类名!");
[self registerClass:NSClassFromString(className) forDecorationViewOfKind:className];
} else {
[self registerClass:[ZLCollectionReusableView class] forDecorationViewOfKind:@"ZLCollectionReusableView"];
}
} else {
[self registerClass:[ZLCollectionReusableView class] forDecorationViewOfKind:@"ZLCollectionReusableView"];
}
x = edgeInsets.left;
y = [self maxHeightWithSection:index];
// 添加页首属性
if (headerH > 0) {
NSIndexPath *headerIndexPath = [NSIndexPath indexPathForItem:0 inSection:index];
ZLCollectionViewLayoutAttributes* headerAttr = [ZLCollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:headerIndexPath];
headerAttr.frame = CGRectMake(0, y, self.collectionView.frame.size.width, headerH);
[headerAttr setValue:[NSValue valueWithCGRect:headerAttr.frame] forKey:@"orginalFrame"];
[self.attributesArray addObject:headerAttr];
[self.headerAttributesArray addObject:headerAttr];
}
y += headerH ;
CGFloat itemStartY = y;
CGFloat lastY = y;
if (itemCount > 0) {
y += edgeInsets.top;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:typeOfLayout:)]) {
self.layoutType = [self.delegate collectionView:self.collectionView layout:self typeOfLayout:index];
}
//NSInteger columnCount = 1;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:columnCountOfSection:)]) {
self.columnCount = [self.delegate collectionView:self.collectionView layout:self columnCountOfSection:index];
}
// 定义一个列高数组 记录每一列的总高度
CGFloat *columnHeight = (CGFloat *) malloc(self.columnCount * sizeof(CGFloat));
CGFloat itemWidth = 0.0;
if (self.layoutType == ClosedLayout) {
for (int i=0; i<self.columnCount; i++) {
columnHeight[i] = y;
}
itemWidth = (totalWidth - edgeInsets.left - edgeInsets.right - minimumInteritemSpacing * (self.columnCount - 1)) / self.columnCount;
}
CGFloat maxYOfPercent = -1;
CGFloat maxYOfFill = y;
NSMutableArray* arrayOfPercent = [NSMutableArray new]; //储存百分比布局的数组
NSMutableArray* arrayOfFill = [NSMutableArray new]; //储存填充式布局的数组
NSMutableArray* arrayOfAbsolute = [NSMutableArray new]; //储存绝对定位布局的数组
NSMutableArray *arrayXOfFill = [NSMutableArray new]; //储存填充式布局的数组
[arrayXOfFill addObject:self.isFloor?@(floor(edgeInsets.left)):@(edgeInsets.left)];
NSMutableArray *arrayYOfFill = [NSMutableArray new]; //储存填充式布局的数组
[arrayYOfFill addObject:self.isFloor?@(floor(maxYOfFill)):@(maxYOfFill)];
for (int i=0; i<itemCount; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:index];
CGSize itemSize = CGSizeZero;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:)]) {
itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
} else {
itemSize = self.itemSize;
}
ZLCollectionViewLayoutAttributes *attributes = [ZLCollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
NSInteger preRow = self.attributesArray.count - 1;
switch (self.layoutType) {
#pragma mark 标签布局处理
case LabelLayout: {
//找上一个cell
if(preRow >= 0){
if(i > 0) {
ZLCollectionViewLayoutAttributes *preAttr = self.attributesArray[preRow];
x = preAttr.frame.origin.x + preAttr.frame.size.width + minimumInteritemSpacing;
if (x + itemSize.width > totalWidth - edgeInsets.right) {
x = edgeInsets.left;
y += itemSize.height + minimumLineSpacing;
}
}
}
if (itemSize.width > (totalWidth-edgeInsets.left-edgeInsets.right)) {
itemSize.width = (totalWidth-edgeInsets.left-edgeInsets.right);
}
attributes.frame = CGRectMake(x, y, itemSize.width, itemSize.height);
}
break;
#pragma mark 列布局处理
case ClosedLayout: {
CGFloat max = CGFLOAT_MAX;
NSInteger column = 0;
for (int i = 0; i < self.columnCount; i++) {
if (columnHeight[i] < max) {
max = columnHeight[i];
column = i;
}
}
CGFloat itemX = edgeInsets.left + (itemWidth+minimumInteritemSpacing)*column;
CGFloat itemY = columnHeight[column];
attributes.frame = CGRectMake(itemX, itemY, itemWidth, itemSize.height);
columnHeight[column] += (itemSize.height + minimumLineSpacing);
}
break;
#pragma mark 百分比布局处理
case PercentLayout: {
CGFloat percent = 0.0f;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:percentOfRow:)]) {
percent = [self.delegate collectionView:self.collectionView layout:self percentOfRow:indexPath];
} else {
percent = 1;
}
if (percent > 1 || percent <= 0) {
percent = 1;
}
if (arrayOfPercent.count > 0) {
CGFloat totalPercent = 0;
for (NSDictionary* dic in arrayOfPercent) {
totalPercent += [dic[@"percent"] floatValue];
}
if ((totalPercent+percent) >= 1.0) {
if ((totalPercent+percent) < 1.1) {
//小于1.1就当成一行来计算
//先添加进总的数组
attributes.indexPath = indexPath;
attributes.frame = CGRectMake(0, 0, (itemSize.width>self.collectionView.frame.size.width-edgeInsets.left-edgeInsets.right)?self.collectionView.frame.size.width-edgeInsets.left-edgeInsets.right:itemSize.width, itemSize.height);
//再添加进计算比例的数组
[arrayOfPercent addObject:[NSMutableDictionary dictionaryWithDictionary:@{@"item":attributes,@"percent":[NSNumber numberWithFloat:percent],@"indexPath":indexPath}]];
if ((totalPercent+percent) > 1) {
NSMutableDictionary* lastDic = [NSMutableDictionary dictionaryWithDictionary:arrayOfPercent.lastObject];
CGFloat lastPercent = 1.0;
for (NSInteger i=0; i<arrayOfPercent.count-1; i++) {
NSMutableDictionary* dic = arrayOfPercent[i];
lastPercent -= [dic[@"percent"] floatValue];
}
lastDic[@"percent"] = [NSNumber numberWithFloat:lastPercent];
[arrayOfPercent replaceObjectAtIndex:arrayOfPercent.count-1 withObject:lastDic];
}
CGFloat realWidth = totalWidth - edgeInsets.left - edgeInsets.right - (arrayOfPercent.count-1)*minimumInteritemSpacing;
for (NSInteger i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes *newAttributes = dic[@"item"];
CGFloat itemX = 0.0f;
if (i==0) {
itemX = edgeInsets.left;
} else {
ZLCollectionViewLayoutAttributes *preAttr = arrayOfPercent[i-1][@"item"];
itemX = preAttr.frame.origin.x + preAttr.frame.size.width + minimumInteritemSpacing;
}
newAttributes.frame = CGRectMake(itemX, (maxYOfPercent==-1)?y:maxYOfPercent+minimumLineSpacing, realWidth*[dic[@"percent"] floatValue], newAttributes.frame.size.height);
newAttributes.indexPath = dic[@"indexPath"];
//if (![self.attributesArray containsObject:newAttributes]) {
[self.attributesArray addObject:newAttributes];
//}
}
for (NSInteger i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes *item = dic[@"item"];
if ((item.frame.origin.y + item.frame.size.height) > maxYOfPercent) {
maxYOfPercent = (item.frame.origin.y + item.frame.size.height);
}
}
[arrayOfPercent removeAllObjects];
}
else {
//先添加进总的数组
if (arrayOfPercent.count > 0) {
for (int i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes* attr = dic[@"item"];
if ((attr.frame.origin.y+attr.frame.size.height) > maxYOfPercent ) {
maxYOfPercent = attr.frame.origin.y+attr.frame.size.height;
}
}
}
attributes.indexPath = indexPath;
attributes.frame = CGRectMake(edgeInsets.left, maxYOfPercent, (itemSize.width>self.collectionView.frame.size.width-edgeInsets.left-edgeInsets.right)?self.collectionView.frame.size.width-edgeInsets.left-edgeInsets.right:itemSize.width, itemSize.height);
for (int i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes* attr = dic[@"item"];
attr.indexPath = dic[@"indexPath"];
[self.attributesArray addObject:attr];
}
[arrayOfPercent removeAllObjects];
//再添加进计算比例的数组
[arrayOfPercent addObject:[NSMutableDictionary dictionaryWithDictionary:@{@"item":attributes,@"percent":[NSNumber numberWithFloat:percent],@"indexPath":indexPath}]];
//如果该行item总比例还是小于1,但是item已经是最后一个
if (i==itemCount-1) {
CGFloat realWidth = totalWidth - edgeInsets.left - edgeInsets.right - (arrayOfPercent.count-1)*minimumInteritemSpacing;
for (NSInteger i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes *newAttributes = dic[@"item"];
CGFloat itemX = 0.0f;
if (i==0) {
itemX = edgeInsets.left;
} else {
ZLCollectionViewLayoutAttributes *preAttr = arrayOfPercent[i-1][@"item"];
itemX = preAttr.frame.origin.x + preAttr.frame.size.width + minimumInteritemSpacing;
}
newAttributes.frame = CGRectMake(itemX, (maxYOfPercent==-1)?y:maxYOfPercent+minimumLineSpacing, realWidth*[dic[@"percent"] floatValue], newAttributes.frame.size.height);
newAttributes.indexPath = dic[@"indexPath"];
[self.attributesArray addObject:newAttributes];
}
for (NSInteger i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes *item = dic[@"item"];
if ((item.frame.origin.y + item.frame.size.height) > maxYOfPercent) {
maxYOfPercent = (item.frame.origin.y + item.frame.size.height);
}
}
[arrayOfPercent removeAllObjects];
}
}
}
else {
//先添加进总的数组
attributes.indexPath = indexPath;
NSDictionary* lastDicForPercent = arrayOfPercent[arrayOfPercent.count-1];
ZLCollectionViewLayoutAttributes *lastAttributesForPercent = lastDicForPercent[@"item"];
attributes.frame = CGRectMake(lastAttributesForPercent.frame.origin.x+lastAttributesForPercent.frame.size.width+minimumInteritemSpacing, lastAttributesForPercent.frame.origin.y, itemSize.width, itemSize.height);
//再添加进计算比例的数组
[arrayOfPercent addObject:[NSMutableDictionary dictionaryWithDictionary:@{@"item":attributes,@"percent":[NSNumber numberWithFloat:percent],@"indexPath":indexPath}]];
//如果已经是最后一个
if (i==itemCount-1) {
NSInteger space = arrayOfPercent.count-1;
if (arrayOfPercent.count > 0) {
NSDictionary* dic = arrayOfPercent[0];
BOOL equal = YES;
for (NSDictionary* d in arrayOfPercent) {
if ([dic[@"percent"] floatValue] != [d[@"percent"] floatValue]) {
equal = NO;
break;
}
}
if (equal == YES) {
space = (1/([dic[@"percent"] floatValue]))-1;
}
}
CGFloat realWidth = totalWidth - edgeInsets.left - edgeInsets.right - space*minimumInteritemSpacing;
for (NSInteger i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes *newAttributes = dic[@"item"];
CGFloat itemX = 0.0f;
if (i==0) {
itemX = edgeInsets.left;
} else {
ZLCollectionViewLayoutAttributes *preAttr = arrayOfPercent[i-1][@"item"];
itemX = preAttr.frame.origin.x + preAttr.frame.size.width + minimumInteritemSpacing;
}
newAttributes.frame = CGRectMake(itemX, (maxYOfPercent==-1)?y:maxYOfPercent+minimumLineSpacing, realWidth*[dic[@"percent"] floatValue], newAttributes.frame.size.height);
newAttributes.indexPath = dic[@"indexPath"];
//if (![self.attributesArray containsObject:newAttributes]) {
[self.attributesArray addObject:newAttributes];
//}
}
for (NSInteger i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes *item = dic[@"item"];
if ((item.frame.origin.y + item.frame.size.height) > maxYOfPercent) {
maxYOfPercent = (item.frame.origin.y + item.frame.size.height);
}
}
[arrayOfPercent removeAllObjects];
} else {
}
}
}
else {
//先添加进总的数组
attributes.indexPath = indexPath;
attributes.frame = CGRectMake(edgeInsets.left, (maxYOfPercent==-1)?y:maxYOfPercent+minimumLineSpacing, (itemSize.width>self.collectionView.frame.size.width-edgeInsets.left-edgeInsets.right)?self.collectionView.frame.size.width-edgeInsets.left-edgeInsets.right:itemSize.width, itemSize.height);
//再添加进计算比例的数组
[arrayOfPercent addObject:[NSMutableDictionary dictionaryWithDictionary:@{@"item":attributes,@"percent":[NSNumber numberWithFloat:percent],@"indexPath":indexPath}]];
//如果已经是最后一个
if (i==itemCount-1) {
NSInteger space = arrayOfPercent.count-1;
if (arrayOfPercent.count > 0) {
NSDictionary* dic = arrayOfPercent[0];
BOOL equal = YES;
for (NSDictionary* d in arrayOfPercent) {
if ([dic[@"percent"] floatValue] != [d[@"percent"] floatValue]) {
equal = NO;
break;
}
}
if (equal == YES) {
space = (1/([dic[@"percent"] floatValue]))-1;
}
}
CGFloat realWidth = totalWidth - edgeInsets.left - edgeInsets.right - space*minimumInteritemSpacing;
for (NSInteger i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes *newAttributes = dic[@"item"];
CGFloat itemX = 0.0f;
if (i==0) {
itemX = edgeInsets.left;
} else {
ZLCollectionViewLayoutAttributes *preAttr = arrayOfPercent[i-1][@"item"];
itemX = preAttr.frame.origin.x + preAttr.frame.size.width + minimumInteritemSpacing;
}
newAttributes.frame = CGRectMake(itemX, (maxYOfPercent==-1)?y:maxYOfPercent+minimumLineSpacing, realWidth*[dic[@"percent"] floatValue], newAttributes.frame.size.height);
newAttributes.indexPath = dic[@"indexPath"];
[self.attributesArray addObject:newAttributes];
}
for (NSInteger i=0; i<arrayOfPercent.count; i++) {
NSDictionary* dic = arrayOfPercent[i];
ZLCollectionViewLayoutAttributes *item = dic[@"item"];
if ((item.frame.origin.y + item.frame.size.height) > maxYOfPercent) {
maxYOfPercent = (item.frame.origin.y + item.frame.size.height);
}
}
[arrayOfPercent removeAllObjects];
} else {
}
}
}
break;
#pragma mark 填充布局处理
case FillLayout: {
BOOL qualified = YES;
if (arrayOfFill.count == 0) {
attributes.frame = CGRectMake(self.isFloor?floor(edgeInsets.left):edgeInsets.left, self.isFloor?floor(maxYOfFill):maxYOfFill, self.isFloor?floor(itemSize.width):itemSize.width, self.isFloor?floor(itemSize.height):itemSize.height);
[arrayOfFill addObject:attributes];
} else {
BOOL leftQualified = NO;
BOOL topQualified = NO;
for (NSNumber* yFill in arrayYOfFill) {
for (NSNumber* xFill in arrayXOfFill) {
qualified = YES;
CGFloat attrX = floor([xFill floatValue])==floor(edgeInsets.left)?floor([xFill floatValue]):(floor([xFill floatValue])+minimumInteritemSpacing);
CGFloat attrY = floor([yFill floatValue])==floor(maxYOfFill)?(self.isFloor?floor([yFill floatValue]):floor([yFill floatValue])):floor([yFill floatValue])+floor(minimumLineSpacing);
attributes.frame = CGRectMake(attrX, attrY, self.isFloor?floor(itemSize.width):itemSize.width, self.isFloor?floor(itemSize.height):itemSize.height);
if (floor(attributes.frame.origin.x)+floor(attributes.frame.size.width) > floor(totalWidth)-floor(edgeInsets.right)) {
qualified = NO;
break;
}
for (ZLCollectionViewLayoutAttributes* attr in arrayOfFill) {
if (CGRectIntersectsRect(attributes.frame, attr.frame)) {
qualified = NO;
break;
}
}
if (qualified == NO) {
continue;
} else {
// 对比左侧的cell
CGPoint leftPt = CGPointMake(attributes.frame.origin.x - floor(minimumInteritemSpacing), attributes.frame.origin.y);
CGRect leftRect = CGRectZero;
for (ZLCollectionViewLayoutAttributes* attr in arrayOfFill) {
if (CGRectContainsPoint(attr.frame, leftPt)) {
leftRect = attr.frame;
break;
}
}
if (CGRectEqualToRect(leftRect, CGRectZero)) {
leftQualified = YES;
} else {
if (attributes.frame.origin.x - (leftRect.origin.x + leftRect.size.width) >= floor(minimumInteritemSpacing)) {
leftQualified = YES;
} else if (floor(leftRect.origin.x) + floor(leftRect.size.width) <= leftPt.x) {
leftQualified = YES;
} else {
CGRect rc = attributes.frame;
rc.origin.x = leftRect.origin.x + leftRect.size.width + floor(minimumInteritemSpacing);
attributes.frame = rc;
for (ZLCollectionViewLayoutAttributes* attr in arrayOfFill) {
if (CGRectIntersectsRect(attributes.frame, attr.frame)) {
qualified = NO;
break;
}
}
}
}
// 对比上侧的cell
CGPoint topPt = CGPointMake(attributes.frame.origin.x, attributes.frame.origin.y - floor(minimumLineSpacing));
CGRect topRect = CGRectZero;
for (ZLCollectionViewLayoutAttributes* attr in arrayOfFill) {
if (CGRectContainsPoint(attr.frame, topPt)) {
topRect = attr.frame;
break;
}
}
if (CGRectEqualToRect(topRect, CGRectZero)) {
topQualified = YES;
} else {
if (attributes.frame.origin.y - (topRect.origin.y + topRect.size.height) >= floor(minimumLineSpacing)) {
topQualified = YES;
} else if (floor(topRect.origin.y) + floor(topRect.size.height) <= topPt.y) {
topQualified = YES;
} else {
CGRect rc = attributes.frame;
rc.origin.y = topRect.origin.y + topRect.size.height + floor(minimumLineSpacing);
attributes.frame = rc;
for (ZLCollectionViewLayoutAttributes* attr in arrayOfFill) {
if (CGRectIntersectsRect(attributes.frame, attr.frame)) {
qualified = NO;
break;
}
}
}
}
if (leftQualified == YES && topQualified == YES) {
qualified = YES;
break;
}
}
}
if (qualified == YES) {
break;
}
}
if (qualified == YES) {
//NSLog(@"第%d个,合格的矩形区域=%@",i,NSStringFromCGRect(attributes.frame));
[arrayOfFill addObject:attributes];
}
}
if (qualified == YES) {
if (![arrayXOfFill containsObject:self.isFloor?@(floor(attributes.frame.origin.x)):@(attributes.frame.origin.x)]) {
[arrayXOfFill addObject:self.isFloor?@(floor(attributes.frame.origin.x)):@(attributes.frame.origin.x)];
}
if (![arrayXOfFill containsObject:self.isFloor?@(floor(attributes.frame.origin.x+attributes.frame.size.width)):@(attributes.frame.origin.x+attributes.frame.size.width)]) {
[arrayXOfFill addObject:self.isFloor?@(floor(attributes.frame.origin.x+attributes.frame.size.width)):@(attributes.frame.origin.x+attributes.frame.size.width)];
}
if (![arrayYOfFill containsObject:self.isFloor?@(floor(attributes.frame.origin.y)):@(attributes.frame.origin.y)]) {
[arrayYOfFill addObject:self.isFloor?@(floor(attributes.frame.origin.y)):@(attributes.frame.origin.y)];
}
if (![arrayYOfFill containsObject:self.isFloor?@(floor(attributes.frame.origin.y+attributes.frame.size.height)):@(attributes.frame.origin.y+attributes.frame.size.height)]) {
[arrayYOfFill addObject:self.isFloor?@(floor(attributes.frame.origin.y+attributes.frame.size.height)):@(attributes.frame.origin.y+attributes.frame.size.height)];
}
[arrayXOfFill sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 floatValue] > [obj2 floatValue];
}];
[arrayYOfFill sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 floatValue] > [obj2 floatValue];
}];
}
}
break;
#pragma mark 绝对定位布局处理
case AbsoluteLayout: {
CGRect itemFrame = CGRectZero;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:rectOfItem:)]) {
itemFrame = [self.delegate collectionView:self.collectionView layout:self rectOfItem:indexPath];
}
CGFloat absolute_x = edgeInsets.left+itemFrame.origin.x;
CGFloat absolute_y = y+itemFrame.origin.y;
CGFloat absolute_w = itemFrame.size.width;
if ((absolute_x+absolute_w>self.collectionView.frame.size.width-edgeInsets.right)&&(absolute_x<self.collectionView.frame.size.width-edgeInsets.right)) {
absolute_w -= (absolute_x+absolute_w-(self.collectionView.frame.size.width-edgeInsets.right));
}
CGFloat absolute_h = itemFrame.size.height;
attributes.frame = CGRectMake(absolute_x, absolute_y, absolute_w, absolute_h);
[arrayOfAbsolute addObject:attributes];
}
break;
default: {
//NSLog(@"%@",NSStringFromCGRect(attributes.frame));
}
break;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:transformOfItem:)]) {
attributes.transform3D = [self.delegate collectionView:self.collectionView layout:self transformOfItem:indexPath];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:zIndexOfItem:)]) {
attributes.zIndex = [self.delegate collectionView:self.collectionView layout:self zIndexOfItem:indexPath];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:alphaOfItem:)]) {
attributes.alpha = [self.delegate collectionView:self.collectionView layout:self alphaOfItem:indexPath];
}
attributes.indexPath = indexPath;
if (self.layoutType != PercentLayout) {
//if (![self.attributesArray containsObject:attributes]) {
[self.attributesArray addObject:attributes];
//}
}
if (self.layoutType == ClosedLayout) {
CGFloat max = 0;
for (int i = 0; i < self.columnCount; i++) {
if (columnHeight[i] > max) {
max = columnHeight[i];
}
}
lastY = max;
} else if (self.layoutType == PercentLayout) {
lastY = maxYOfPercent;
} else if (self.layoutType == FillLayout) {
if (i==itemCount-1) {
for (ZLCollectionViewLayoutAttributes* attr in arrayOfFill) {
if (maxYOfFill < attr.frame.origin.y+attr.frame.size.height) {
maxYOfFill = attr.frame.origin.y+attr.frame.size.height;
}
}
}
lastY = maxYOfFill;
} else if (self.layoutType == AbsoluteLayout) {
if (i==itemCount-1) {
for (ZLCollectionViewLayoutAttributes* attr in arrayOfAbsolute) {
if (lastY < attr.frame.origin.y+attr.frame.size.height) {
lastY = attr.frame.origin.y+attr.frame.size.height;
}
}
}
} else {
lastY = attributes.frame.origin.y + attributes.frame.size.height;
}
}
free(columnHeight);
}
if (self.layoutType == ClosedLayout) {
if (itemCount > 0) {
lastY -= minimumLineSpacing;
}
}
if (itemCount > 0) {
lastY += edgeInsets.bottom;
}
// 添加页脚属性
if (footerH > 0) {
NSIndexPath *footerIndexPath = [NSIndexPath indexPathForItem:0 inSection:index];
ZLCollectionViewLayoutAttributes *footerAttr = [ZLCollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:footerIndexPath];
footerAttr.frame = CGRectMake(0, lastY, self.collectionView.frame.size.width, footerH);
[self.attributesArray addObject:footerAttr];
lastY += footerH;
}
#pragma mark 添加背景图
CGFloat backHeight = lastY-itemStartY+([self isAttachToTop:index]?headerH:0)-([self isAttachToBottom:index]?0:footerH);
if (backHeight < 0) {
backHeight = 0;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:registerBackView:)]) {
NSString* className = [self.delegate collectionView:self.collectionView layout:self registerBackView:index];
if (className != nil && className.length > 0) {
ZLCollectionViewBackgroundViewLayoutAttributes *attr = [ZLCollectionViewBackgroundViewLayoutAttributes layoutAttributesForDecorationViewOfKind:className withIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]];
attr.frame = CGRectMake(0, [self isAttachToTop:index]?itemStartY-headerH:itemStartY, self.collectionView.frame.size.width, backHeight);
attr.zIndex = -1000;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backgroundViewMethodForSection:)]) {
if ([self.delegate collectionView:self.collectionView layout:self backgroundViewMethodForSection:index] != nil) {
[attr callMethod:[self.delegate collectionView:self.collectionView layout:self backgroundViewMethodForSection:index]];
}
}
[self.attributesArray addObject:attr];
} else {
ZLCollectionViewLayoutAttributes *attr = [ZLCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:@"ZLCollectionReusableView" withIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]];
attr.frame = CGRectMake(0, [self isAttachToTop:index]?itemStartY-headerH:itemStartY, self.collectionView.frame.size.width, backHeight);
attr.color = self.collectionView.backgroundColor;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backColorForSection:)]) {
attr.color = [self.delegate collectionView:self.collectionView layout:self backColorForSection:index];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backImageForSection:)]) {
attr.image = [self.delegate collectionView:self.collectionView layout:self backImageForSection:index];
}
attr.zIndex = -1000;
[self.attributesArray addObject:attr];
}
} else {
ZLCollectionViewLayoutAttributes *attr = [ZLCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:@"ZLCollectionReusableView" withIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]];
attr.frame = CGRectMake(0, [self isAttachToTop:index]?itemStartY-headerH:itemStartY, self.collectionView.frame.size.width, backHeight);
attr.color = self.collectionView.backgroundColor;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backColorForSection:)]) {
attr.color = [self.delegate collectionView:self.collectionView layout:self backColorForSection:index];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backImageForSection:)]) {
attr.image = [self.delegate collectionView:self.collectionView layout:self backImageForSection:index];
}
attr.zIndex = -1000;
[self.attributesArray addObject:attr];
}
self.collectionHeightsArray[index] = [NSNumber numberWithFloat:lastY];
}
// for (ZLCollectionViewLayoutAttributes* attr in self.attributesArray) {
// NSLog(@"类型=%@,尺寸=%@",attr.representedElementKind, NSStringFromCGRect(attr.frame));
// }
[self forceSetIsNeedReCalculateAllLayout:NO];
}
#pragma mark - CollectionView的滚动范围
- (CGSize)collectionViewContentSize
{
if (self.collectionHeightsArray.count <= 0) {
return CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
}
CGFloat footerH = 0.0f;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForFooterInSection:)]) {
footerH = [self.delegate collectionView:self.collectionView layout:self referenceSizeForFooterInSection:self.collectionHeightsArray.count-1].height;
} else {
footerH = self.footerReferenceSize.height;
}
UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
edgeInsets = [self.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:self.collectionHeightsArray.count-1];
} else {
edgeInsets = self.sectionInset;
}
return CGSizeMake(self.collectionView.frame.size.width, [self.collectionHeightsArray[self.collectionHeightsArray.count-1] floatValue]);// + edgeInsets.bottom + footerH);
}
/**
每个区的初始Y坐标
@param section 区索引
@return Y坐标
*/
- (CGFloat)maxHeightWithSection:(NSInteger)section {
if (section>0) {
return [self.collectionHeightsArray[section-1] floatValue];
} else {
return 0;
}
}
- (BOOL)isAttachToTop:(NSInteger)section {
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:attachToTop:)]) {
return [self.delegate collectionView:self.collectionView layout:self attachToTop:section];
}
return NO;
}
- (BOOL)isAttachToBottom:(NSInteger)section {
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:attachToBottom:)]) {
return [self.delegate collectionView:self.collectionView layout:self attachToBottom:section];
}
return NO;
}
@end