forked from Tech-Chao/ULBCollectionViewFlowLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ULBCollectionViewFlowLayout.m
123 lines (89 loc) · 4.4 KB
/
ULBCollectionViewFlowLayout.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
//
// ULBCollectionViewFlowLayout.m
// uliaobao
//
// Created by FishYu on 16/8/24.
// Copyright © 2016年 CGC. All rights reserved.
//
#import "ULBCollectionViewFlowLayout.h"
static NSString *const ULBCollectionViewSectionColor = @"com.ulb.ULBCollectionElementKindSectionColor";
@interface ULBCollectionViewLayoutAttributes : UICollectionViewLayoutAttributes
// 背景色
@property (nonatomic, strong) UIColor *backgroudColor;
@end
@implementation ULBCollectionViewLayoutAttributes
@end
@interface ULBCollectionReusableView : UICollectionReusableView
@end
@implementation ULBCollectionReusableView
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
[super applyLayoutAttributes:layoutAttributes];
ULBCollectionViewLayoutAttributes *attr = (ULBCollectionViewLayoutAttributes *)layoutAttributes;
self.backgroundColor = attr.backgroudColor;
}
@end
@interface ULBCollectionViewFlowLayout ()
@property (nonatomic, strong) UIColor *sectonColor;
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *decorationViewAttrs;
@end
@implementation ULBCollectionViewFlowLayout
- (void)prepareLayout{
[super prepareLayout];
NSInteger sections = [self.collectionView numberOfSections];
id<ULBCollectionViewDelegateFlowLayout> delegate = self.collectionView.delegate;
if ([delegate respondsToSelector:@selector(collectionView:layout:colorForSectionAtIndex:)]) {
}else{
return ;
}
//1.初始化
[self registerClass:[ULBCollectionReusableView class] forDecorationViewOfKind:ULBCollectionViewSectionColor];
[self.decorationViewAttrs removeAllObjects];
for (NSInteger section =0; section < sections; section++) {
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section];
if (numberOfItems > 0) {
UICollectionViewLayoutAttributes *firstAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
UICollectionViewLayoutAttributes *lastAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:(numberOfItems - 1) inSection:section]];
UIEdgeInsets sectionInset = self.sectionInset;
if ([delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
UIEdgeInsets inset = [delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
if (!UIEdgeInsetsEqualToEdgeInsets(inset, sectionInset)) {
sectionInset = inset;
}
}
CGRect sectionFrame = CGRectUnion(firstAttr.frame, lastAttr.frame);
sectionFrame.origin.x -= sectionInset.left;
sectionFrame.origin.y -= sectionInset.top;
if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
sectionFrame.size.width += sectionInset.left + sectionInset.right;
sectionFrame.size.height = self.collectionView.frame.size.height;
}else{
sectionFrame.size.width = self.collectionView.frame.size.width;
sectionFrame.size.height += sectionInset.top + sectionInset.bottom;
}
//2. 定义
ULBCollectionViewLayoutAttributes *attr = [ULBCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:ULBCollectionViewSectionColor withIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
attr.frame = sectionFrame;
attr.zIndex = -1;
attr.backgroudColor = [delegate collectionView:self.collectionView layout:self colorForSectionAtIndex:section];
[self.decorationViewAttrs addObject:attr];
}else{
continue ;
}
}
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
NSMutableArray * attrs = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for (UICollectionViewLayoutAttributes *attr in self.decorationViewAttrs) {
if (CGRectIntersectsRect(rect, attr.frame)) {
[attrs addObject:attr];
}
}
return [attrs copy];
}
- (NSMutableArray<UICollectionViewLayoutAttributes *> *)decorationViewAttrs{
if (!_decorationViewAttrs) {
_decorationViewAttrs = [NSMutableArray array];
}
return _decorationViewAttrs;
}
@end