forked from steipete/PSTCollectionView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSTGridLayoutRow.m
More file actions
205 lines (181 loc) · 7.67 KB
/
PSTGridLayoutRow.m
File metadata and controls
205 lines (181 loc) · 7.67 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
//
// PSTGridLayoutRow.m
// PSPDFKit
//
// Copyright (c) 2012 Peter Steinberger. All rights reserved.
//
#import "PSTCollectionView.h"
#import "PSTGridLayoutRow.h"
#import "PSTGridLayoutSection.h"
#import "PSTGridLayoutItem.h"
#import "PSTGridLayoutInfo.h"
#import "PSTCollectionViewFlowLayout.h"
@interface PSTGridLayoutRow() {
NSMutableArray *_items;
BOOL _isValid;
int _verticalAlignement;
int _horizontalAlignement;
}
@property (nonatomic, strong) NSArray *items;
@end
@implementation PSTGridLayoutRow
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSObject
- (id)init {
if((self = [super init])) {
_items = [NSMutableArray new];
}
return self;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p frame:%@ index:%d items:%@>", NSStringFromClass([self class]), self, NSStringFromCGRect(self.rowFrame), self.index, self.items];
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Public
- (void)invalidate {
_isValid = NO;
_rowSize = CGSizeZero;
_rowFrame = CGRectZero;
}
- (NSArray *)itemRects {
return [self layoutRowAndGenerateRectArray:YES];
}
- (void)layoutRow {
[self layoutRowAndGenerateRectArray:NO];
}
- (NSArray *)layoutRowAndGenerateRectArray:(BOOL)generateRectArray {
NSMutableArray *rects = generateRectArray ? [NSMutableArray array] : nil;
if (!_isValid || generateRectArray) {
// properties for aligning
BOOL isHorizontal = self.section.layoutInfo.horizontal;
BOOL isLastRow = self.section.indexOfImcompleteRow == self.index;
PSTFlowLayoutHorizontalAlignment horizontalAlignment = [self.section.rowAlignmentOptions[isLastRow ? PSTFlowLayoutLastRowHorizontalAlignmentKey : PSTFlowLayoutCommonRowHorizontalAlignmentKey] integerValue];
// calculate space that's left over if we would align it from left to right.
CGFloat leftOverSpace = self.section.layoutInfo.dimension;
if (isHorizontal) {
leftOverSpace -= self.section.sectionMargins.top + self.section.sectionMargins.bottom;
}else {
leftOverSpace -= self.section.sectionMargins.left + self.section.sectionMargins.right;
}
// calculate the space that we have left after counting all items.
// UICollectionView is smart and lays out items like they would have been placed on a full row
// So we need to calculate the "usedItemCount" with using the last item as a reference size.
// This allows us to correctly justify-place the items in the grid.
NSUInteger usedItemCount = 0;
NSInteger itemIndex = 0;
BOOL canFitMoreItems = itemIndex < self.itemCount;
while (itemIndex < self.itemCount || canFitMoreItems) {
if (!self.fixedItemSize) {
PSTGridLayoutItem *item = self.items[MIN(itemIndex, self.itemCount-1)];
leftOverSpace -= isHorizontal ? item.itemFrame.size.height : item.itemFrame.size.width;
canFitMoreItems = isHorizontal ? leftOverSpace > item.itemFrame.size.height : leftOverSpace > item.itemFrame.size.width;
}else {
leftOverSpace -= isHorizontal ? self.section.itemSize.height : self.section.itemSize.width;
canFitMoreItems = isHorizontal ? leftOverSpace > self.section.itemSize.height : leftOverSpace > self.section.itemSize.width;
}
// separator starts after first item
if (itemIndex > 0) {
leftOverSpace -= isHorizontal ? self.section.verticalInterstice : self.section.horizontalInterstice;
}
itemIndex++;
usedItemCount = itemIndex;
}
CGPoint itemOffset = CGPointZero;
if (horizontalAlignment == PSTFlowLayoutHorizontalAlignmentRight) {
itemOffset.x += leftOverSpace;
}else if(horizontalAlignment == PSTFlowLayoutHorizontalAlignmentCentered) {
itemOffset.x += leftOverSpace/2;
}
// calculate row frame as union of all items
CGRect frame = CGRectZero;
CGRect itemFrame = (CGRect){.size=self.section.itemSize};
for (itemIndex = 0; itemIndex < self.itemCount; itemIndex++) {
PSTGridLayoutItem *item = nil;
if (!self.fixedItemSize) {
item = self.items[itemIndex];
itemFrame = [item itemFrame];
}
if (isHorizontal) {
itemFrame.origin.y = itemOffset.y;
itemOffset.y += itemFrame.size.height + self.section.verticalInterstice;
if (horizontalAlignment == PSTFlowLayoutHorizontalAlignmentJustify) {
itemOffset.y += leftOverSpace/(CGFloat)(usedItemCount-1);
}
}else {
itemFrame.origin.x = itemOffset.x;
itemOffset.x += itemFrame.size.width + self.section.horizontalInterstice;
if (horizontalAlignment == PSTFlowLayoutHorizontalAlignmentJustify) {
itemOffset.x += leftOverSpace/(CGFloat)(usedItemCount-1);
}
}
item.itemFrame = CGRectIntegral(itemFrame); // might call nil; don't care
[rects addObject:[NSValue valueWithCGRect:CGRectIntegral(itemFrame)]];
frame = CGRectUnion(frame, itemFrame);
}
_rowSize = frame.size;
// _rowFrame = frame; // set externally
_isValid = YES;
}
return rects;
}
- (void)addItem:(PSTGridLayoutItem *)item {
[_items addObject:item];
item.rowObject = self;
[self invalidate];
}
- (PSTGridLayoutRow *)snapshot {
PSTGridLayoutRow *snapshotRow = [[self class] new];
snapshotRow.section = self.section;
snapshotRow.items = self.items;
snapshotRow.rowSize = self.rowSize;
snapshotRow.rowFrame = self.rowFrame;
snapshotRow.index = self.index;
snapshotRow.complete = self.complete;
snapshotRow.fixedItemSize = self.fixedItemSize;
snapshotRow.itemCount = self.itemCount;
return snapshotRow;
}
- (PSTGridLayoutRow *)copyFromSection:(PSTGridLayoutSection *)section {
return nil; // ???
}
- (NSInteger)itemCount {
if(self.fixedItemSize) {
return _itemCount;
}else {
return [self.items count];
}
}
@end
/*
// _UIGridLayoutItem
#import <objc/runtime.h>
#import <objc/message.h>
__attribute__((constructor)) static void PSHackUIGridLayoutItem(void) {
@autoreleasepool {
SEL itemFrameSetter = @selector(ps_dealloc);
IMP customImageViewDescIMP = imp_implementationWithBlock(PSBlockImplCast(^(__unsafe_unretained id _self)) {
// NSLog(@"deallocating %@", _self);
objc_msgSend(_self, itemFrameSetter);
});
PSPDFReplaceMethod(NSClassFromString(@"UICollectionViewLayoutAttributes"), NSSelectorFromString(@"dealloc"), itemFrameSetter, customImageViewDescIMP);
}
}
__attribute__((constructor)) static void PSHackUIGridLayoutSection(void) {
@autoreleasepool {
SEL itemFrameSetter = @selector(ps_addRow);
IMP customImageViewDescIMP = imp_implementationWithBlock(^(id _self) {
objc_msgSend(_self, itemFrameSetter);
});
PSPDFReplaceMethod(NSClassFromString(@"_UIGridLayoutSection"), @selector(addRow), itemFrameSetter, customImageViewDescIMP);
}
}
__attribute__((constructor)) static void PSHackUICollectionViewLayoutAttributes(void) {
@autoreleasepool {
SEL itemFrameSetter = @selector(ps_initWithCollectionView:layout:);
IMP customImageViewDescIMP = imp_implementationWithBlock(^(id _self, id c, id l) {
return objc_msgSend(_self, itemFrameSetter, c, l);
});
PSPDFReplaceMethod(NSClassFromString(@"UICollectionViewData"), @selector(initWithCollectionView:layout:), itemFrameSetter, customImageViewDescIMP);
}
}
*/