-
Notifications
You must be signed in to change notification settings - Fork 1
/
JRGridLayoutWithStickyCellsLayout.m
248 lines (217 loc) · 9.18 KB
/
JRGridLayoutWithStickyCellsLayout.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
#import "JRGridLayoutWithStickyCellsLayout.h"
@interface JRGridLayoutWithStickyCellsLayout()
@property (nonatomic, strong) NSArray* columnWidths;
@property (nonatomic, strong) NSArray* rowHeights;
@property (nonatomic, strong) NSArray* columnOriginsX;
@property (nonatomic, strong) NSArray* rowOriginsY;
@property (nonatomic, assign) BOOL hasColumnRowsAndHeights;
@property (nonatomic, assign) BOOL hasDynamicRowHeight;
@property (nonatomic, assign) BOOL hasDynamicColumnWidth;
@end
@implementation JRGridLayoutWithStickyCellsLayout
-(void) getColumnAndRowHeights{
self.hasDynamicRowHeight = [self.collectionView.delegate respondsToSelector:@selector(collectionView:heightForRowAtIndex:)];
self.hasDynamicColumnWidth = [self.collectionView.delegate respondsToSelector:@selector(collectionView:widthForColumAtIndex:)];
if(self.hasDynamicRowHeight){
CGFloat offset = 0;
NSMutableArray* array = [NSMutableArray array];
NSMutableArray* origins = [NSMutableArray array];
for (NSUInteger section = 0; section < self.collectionView.numberOfSections; section ++) {
CGFloat height = [((id<JRGridLayoutWithStickyCellsLayoutDelegate>)(self.collectionView.delegate)) collectionView:self.collectionView heightForRowAtIndex:section];
[array addObject:@(height)];
[origins addObject:@(offset)];
offset += height;
}
self.rowHeights = array;
self.rowOriginsY = origins;
}
if(self.hasDynamicColumnWidth){
CGFloat offset = 0;
NSMutableArray* array = [NSMutableArray array];
NSMutableArray* origins = [NSMutableArray array];
for (NSUInteger column = 0; column < [self.collectionView numberOfItemsInSection:0]; column ++) {
CGFloat height = [((id<JRGridLayoutWithStickyCellsLayoutDelegate>)(self.collectionView.delegate)) collectionView:self.collectionView widthForColumAtIndex:column];
[array addObject:@(height)];
[origins addObject:@(offset)];
offset += height;
}
self.columnWidths = array;
self.columnOriginsX = origins;
}
self.hasColumnRowsAndHeights = YES;
}
-(CGFloat) columnWidthAtIndex:(NSInteger)index{
if (self.hasDynamicColumnWidth) {
return [self.columnWidths[index] doubleValue];
}else{
return self.itemSize.width;
}
}
-(CGFloat) rowHeightAtIndex:(NSInteger) index{
if (self.hasDynamicRowHeight) {
return [self.rowHeights[index] doubleValue];
}else{
return self.itemSize.height;
}
}
-(CGFloat) originXForColumnAtIndex:(NSInteger) column{
CGFloat x = 0;
if (self.hasDynamicColumnWidth) {
x = [self.columnOriginsX[column] doubleValue];
}else{
x = column * self.itemSize.width;
}
return x;
}
-(CGFloat) originYForRowAtIndex:(NSInteger) row{
CGFloat y =0;
if (self.hasDynamicRowHeight) {
y = [self.rowOriginsY[row] doubleValue];
}else{
y = row * self.itemSize.height;
}
return y;
}
-(CGRect) frameForItemAtIndexPath:(NSIndexPath*) indexPath{
CGRect frame = CGRectZero;
frame.origin.x = [self originXForColumnAtIndex:indexPath.row];
frame.origin.y = [self originYForRowAtIndex:indexPath.section];
frame.size.width = [self columnWidthAtIndex:indexPath.row];
frame.size.height = [self rowHeightAtIndex:indexPath.section];
return frame;
}
-(NSInteger) columnIndexForXPosition:(CGFloat) originX{
if (self.hasDynamicColumnWidth) {
NSInteger index = [self.columnOriginsX indexOfObject:@(originX) inSortedRange:NSMakeRange(0, self.columnOriginsX.count) options:NSBinarySearchingInsertionIndex usingComparator:^NSComparisonResult(NSNumber* _Nonnull obj1, NSNumber* _Nonnull obj2) {
return [obj1 compare:obj2];
}];
return index;
}else{
if (self.itemSize.width > 0) {
return (NSInteger)floor(originX/self.itemSize.width);
}
}
return 0;
}
-(NSInteger) rowIndexForYPosition:(CGFloat) originY{
if (self.hasDynamicRowHeight) {
NSInteger index = [self.rowOriginsY indexOfObject:@(originY) inSortedRange:NSMakeRange(0, self.rowOriginsY.count) options:NSBinarySearchingInsertionIndex usingComparator:^NSComparisonResult(NSNumber* _Nonnull obj1, NSNumber* _Nonnull obj2) {
return [obj1 compare:obj2];
}];
return index;
}else{
if (self.itemSize.height > 0) {
return (NSInteger)floor(originY/self.itemSize.height);
}
}
return 0;
}
-(void) getColumnAndRowHeightsIfNeeded{
if (!self.hasColumnRowsAndHeights) {
[self getColumnAndRowHeights];
}
}
-(void) invalidateLayout{
[super invalidateLayout];
}
-(void) invalidateLayoutWithContext:(UICollectionViewLayoutInvalidationContext *)context{
[super invalidateLayoutWithContext:context];
if(context.invalidatedItemIndexPaths || context.invalidateEverything || context.invalidateDataSourceCounts ){
self.hasColumnRowsAndHeights = NO;
[self getColumnAndRowHeights];
}
}
-(CGSize) collectionViewContentSize{
[self getColumnAndRowHeightsIfNeeded];
CGFloat width = 0;
if (self.hasDynamicColumnWidth) {
width = [[self.columnOriginsX lastObject] doubleValue] + [[self.columnWidths lastObject] doubleValue];
}else{
width = self.itemSize.width * [self.collectionView numberOfItemsInSection:0];
}
CGFloat height = 0;
if (self.hasDynamicRowHeight) {
height = [[self.rowOriginsY lastObject] doubleValue] + [[self.rowHeights lastObject] doubleValue];
}else{
height = self.itemSize.height * [self.collectionView numberOfSections];
}
return CGSizeMake(width, height);
}
-(UICollectionViewLayoutAttributes*) layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
[self getColumnAndRowHeightsIfNeeded];
UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGRect frame = [self frameForItemAtIndexPath:indexPath];
attributes.zIndex = 0;
if (indexPath.row < self.columnsToStickOnLeft) {
attributes.zIndex+= 1;
if (self.collectionView.contentOffset.x > 0) {
frame.origin.x += self.collectionView.contentOffset.x;
}
CGFloat adjustment = self.collectionView.contentOffset.x + self.collectionView.contentInset.left;
if (adjustment < 0) {
adjustment = 0;
}
if (adjustment > self.collectionView.contentInset.left) {
adjustment = self.collectionView.contentInset.left;
}
frame.origin.x += adjustment;
}
if (indexPath.section < self.rowsToStickOnTop) {
attributes.zIndex+= 2;
if (self.collectionView.contentOffset.y > 0) {
frame.origin.y += self.collectionView.contentOffset.y;
}
CGFloat adjustment = self.collectionView.contentOffset.y + self.collectionView.contentInset.top;
if (adjustment < 0) {
adjustment = 0;
}
if (adjustment > self.collectionView.contentInset.top) {
adjustment = self.collectionView.contentInset.top;
}
frame.origin.y += adjustment;
}
attributes.frame = frame;
return attributes;
}
-(NSIndexSet*) visibleColumnsInRect:(CGRect) rect{
NSMutableIndexSet* toReturn = [NSMutableIndexSet indexSet];
NSInteger firstColumn = [self columnIndexForXPosition:CGRectGetMinX(rect)];
if (firstColumn > 0) {
firstColumn --;
}
NSInteger lastColumn = [self columnIndexForXPosition:CGRectGetMaxX(rect)] +1;
[toReturn addIndexesInRange:NSMakeRange(firstColumn, lastColumn - firstColumn)];
[toReturn addIndexesInRange:NSMakeRange(0, self.columnsToStickOnLeft)];
return toReturn;
}
- (NSIndexSet*)visibleRowsInRect:(CGRect)rect {
NSMutableIndexSet* toReturn = [NSMutableIndexSet indexSet];
NSInteger firstRow = [self rowIndexForYPosition:CGRectGetMinY(rect)];
if (firstRow > 0) {
firstRow --;
}
NSInteger lastRow = [self rowIndexForYPosition:CGRectGetMaxY(rect)] + 1;
[toReturn addIndexesInRange:NSMakeRange(firstRow, lastRow - firstRow)];
[toReturn addIndexesInRange:NSMakeRange(0, self.rowsToStickOnTop)];
return toReturn;
}
- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
[self getColumnAndRowHeightsIfNeeded];
NSMutableArray* items = [NSMutableArray array];
NSIndexSet* visibleColumns = [self visibleColumnsInRect:rect];
NSIndexSet* visibleRows = [self visibleRowsInRect:rect];
[visibleColumns enumerateIndexesUsingBlock:^(NSUInteger column, BOOL * _Nonnull stop) {
[visibleRows enumerateIndexesUsingBlock:^(NSUInteger row, BOOL * _Nonnull stop) {
if ([self.collectionView numberOfSections] > row) {
if ([self.collectionView numberOfItemsInSection:row] > column) {
[items addObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:column inSection:row]]];
}
}
}];
}];
return items;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
return YES;
}
@end