-
Notifications
You must be signed in to change notification settings - Fork 2
/
TJCollectionViewColumnFlowLayout.m
229 lines (186 loc) · 8.53 KB
/
TJCollectionViewColumnFlowLayout.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
//
// TJCollectionViewColumnFlowLayout.m
//
// Created by Thomas Joulin on 8/18/14.
// Copyright (c) 2014 Thomas Joulin. All rights reserved.
//
#import "TJCollectionViewColumnFlowLayout.h"
@interface TJCollectionViewColumnFlowLayout ()
@property (nonatomic, weak) id <TJCollectionViewColumnFlowDelegateLayout> delegate;
@property (nonatomic, strong) NSMutableArray *itemsAttributes;
@property (nonatomic, strong) NSMutableArray *headersAttributes;
@property (nonatomic, strong) NSMutableArray *footersAttributes;
@property (nonatomic, assign) CGFloat maxX;
@end
@implementation TJCollectionViewColumnFlowLayout
- (void)commonInit
{
_columnSpacing = 0;
_interitemSpacing = 0;
_sectionInset = UIEdgeInsetsZero;
_itemSize = CGSizeMake(320, 44);
_headerReferenceSize = CGSizeZero;
_footerReferenceSize = CGSizeZero;
_headersAttributes = [[NSMutableArray alloc] init];
_footersAttributes = [[NSMutableArray alloc] init];
_itemsAttributes = [[NSMutableArray alloc] init];
}
- (id)init
{
if (self = [super init])
{
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self commonInit];
}
return self;
}
- (void)prepareLayout
{
CGFloat currentY = 0;
NSInteger currentColumn = 0;
NSMutableArray *currentColumnAttributes = [[NSMutableArray alloc] init];
self.maxX = 0;
[self.itemsAttributes removeAllObjects];
[self.headersAttributes removeAllObjects];
[self.footersAttributes removeAllObjects];
for (NSInteger section = 0; section < [self.collectionView numberOfSections]; section++)
{
UICollectionViewLayoutAttributes *attributes;
CGSize headerSize = self.headerReferenceSize;
CGSize footerSize = self.footerReferenceSize;
CGFloat interitemSpacing = self.interitemSpacing;
UIEdgeInsets sectionInsets = self.sectionInset;
NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
NSMutableArray *sectionAttributes = [[NSMutableArray alloc] init];
if ([self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForHeaderInSection:)])
{
headerSize = [self.delegate collectionView:self.collectionView layout:self referenceSizeForHeaderInSection:section];
}
if ([self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForFooterInSection:)])
{
footerSize = [self.delegate collectionView:self.collectionView layout:self referenceSizeForFooterInSection:section];
}
if ([self.delegate respondsToSelector:@selector(collectionView:layout:interitemSpacingForSectionAtIndex:)])
{
interitemSpacing = [self.delegate collectionView:self.collectionView layout:self interitemSpacingForSectionAtIndex:section];
}
if ([self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)])
{
sectionInsets = [self.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
}
if (headerSize.height > 0)
{
attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
attributes.frame = CGRectMake(sectionInsets.left + (currentColumn * headerSize.width), sectionInsets.top + currentY, headerSize.width, headerSize.height);
[self.headersAttributes addObject:attributes];
currentY += sectionInsets.top + headerSize.height;
}
for (NSInteger itemIndex = 0; itemIndex < itemCount; itemIndex++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:itemIndex inSection:section];
CGSize size = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = CGRectMake(sectionInsets.left + (currentColumn * size.width), currentY, size.width, size.height);
self.maxX = fmaxf(attributes.frame.origin.x + attributes.frame.size.width, self.maxX);
[sectionAttributes addObject:attributes];
currentY += size.height + interitemSpacing;
}
if (footerSize.height > 0)
{
attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
attributes.frame = CGRectMake(sectionInsets.left + (currentColumn * footerSize.width), currentY, footerSize.width, footerSize.height);
[self.footersAttributes addObject:attributes];
currentY += footerSize.height + sectionInsets.bottom;
}
currentY += sectionInsets.bottom;
if (currentY > (self.collectionView.bounds.size.height - self.collectionView.contentInset.top - self.collectionView.contentInset.bottom))
{
if (!currentColumnAttributes.count)
{
@throw [NSException exceptionWithName:@"SCTCollectionViewLayoutException"
reason:[NSString stringWithFormat:@"Collection view height (%.2f) too small to fit all items at section %d (needs to be at least %.2f)", self.collectionView.bounds.size.height, section, currentY]
userInfo:nil];
}
[self.itemsAttributes addObjectsFromArray:currentColumnAttributes];
currentColumnAttributes = [[NSMutableArray alloc] init];
currentColumn += 1;
currentY = 0;
section -= 1;
[self.headersAttributes removeLastObject];
[self.footersAttributes removeLastObject];
}
else if (section == [self.collectionView numberOfSections] - 1)
{
[currentColumnAttributes addObjectsFromArray:sectionAttributes];
[self.itemsAttributes addObjectsFromArray:currentColumnAttributes];
}
else
{
[currentColumnAttributes addObjectsFromArray:sectionAttributes];
}
};
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
return self.itemsAttributes[indexPath.row][indexPath.section];
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attribute = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader])
{
attribute = self.headersAttributes[indexPath.section];
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter])
{
attribute = self.footersAttributes[indexPath.section];
}
return attribute;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *attributesArray = [[NSMutableArray alloc] init];
for (UICollectionViewLayoutAttributes *attributes in self.itemsAttributes)
{
if (CGRectIntersectsRect(rect, attributes.frame))
{
[attributesArray addObject:attributes];
}
}
for (UICollectionViewLayoutAttributes *attributes in self.headersAttributes)
{
if (CGRectIntersectsRect(rect, attributes.frame))
{
[attributesArray addObject:attributes];
}
}
for (UICollectionViewLayoutAttributes *attributes in self.footersAttributes)
{
if (CGRectIntersectsRect(rect, attributes.frame))
{
[attributesArray addObject:attributes];
}
}
return [attributesArray copy];
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return CGRectGetWidth(newBounds) != CGRectGetWidth(self.collectionView.bounds);
}
- (CGSize)collectionViewContentSize
{
return CGSizeMake(fmaxf(self.maxX, (self.collectionView.bounds.size.width - self.collectionView.contentInset.left - self.collectionView.contentInset.right)),
self.collectionView.bounds.size.height - self.collectionView.contentInset.top - self.collectionView.contentInset.bottom);
}
- (id<TJCollectionViewColumnFlowDelegateLayout>)delegate
{
return (id<TJCollectionViewColumnFlowDelegateLayout>)self.collectionView.delegate;
}
@end