-
Notifications
You must be signed in to change notification settings - Fork 56
/
PinterestLayout.swift
190 lines (158 loc) · 6.37 KB
/
PinterestLayout.swift
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
//
// PinterestLayout.swift
// PinterestLayout
//
// Created by Roman Sorochak on 7/4/17.
// Copyright © 2017 MagicLab. All rights reserved.
//
import UIKit
/**
PinterestLayout.
*/
public class PinterestLayout: UICollectionViewLayout {
/**
Delegate.
*/
public var delegate: PinterestLayoutDelegate!
/**
Number of columns.
*/
public var numberOfColumns: Int = 1
/**
Cell padding.
*/
public var cellPadding: CGFloat = 0
private var cache = [PinterestLayoutAttributes]()
private var contentHeight: CGFloat = 0
private var contentWidth: CGFloat {
get {
let bounds = collectionView.bounds
let insets = collectionView.contentInset
return bounds.width - insets.left - insets.right
}
}
override public var collectionViewContentSize: CGSize {
get {
return CGSize(
width: contentWidth,
height: contentHeight
)
}
}
override public class var layoutAttributesClass: AnyClass {
return PinterestLayoutAttributes.self
}
override public var collectionView: UICollectionView {
return super.collectionView!
}
private var numberOfSections: Int {
return collectionView.numberOfSections
}
private func numberOfItems(inSection section: Int) -> Int {
return collectionView.numberOfItems(inSection: section)
}
/**
Invalidates layout.
*/
override public func invalidateLayout() {
cache.removeAll()
contentHeight = 0
super.invalidateLayout()
}
override public func prepare() {
if cache.isEmpty {
let collumnWidth = contentWidth / CGFloat(numberOfColumns)
let cellWidth = collumnWidth - (cellPadding * 2)
var xOffsets = [CGFloat]()
for collumn in 0..<numberOfColumns {
xOffsets.append(CGFloat(collumn) * collumnWidth)
}
for section in 0..<numberOfSections {
let numberOfItems = self.numberOfItems(inSection: section)
if let headerSize = delegate.collectionView?(
collectionView: collectionView,
sizeForSectionHeaderViewForSection: section
) {
let headerX = (contentWidth - headerSize.width) / 2
let headerFrame = CGRect(
origin: CGPoint(
x: headerX,
y: contentHeight
),
size: headerSize
)
let headerAttributes = PinterestLayoutAttributes(
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,
with: IndexPath(item: 0, section: section)
)
headerAttributes.frame = headerFrame
cache.append(headerAttributes)
contentHeight = headerFrame.maxY
}
var yOffsets = [CGFloat](
repeating: contentHeight,
count: numberOfColumns
)
for item in 0..<numberOfItems {
let indexPath = IndexPath(item: item, section: section)
let column = yOffsets.index(of: yOffsets.min() ?? 0) ?? 0
let imageHeight = delegate.collectionView(
collectionView: collectionView,
heightForImageAtIndexPath: indexPath,
withWidth: cellWidth
)
let annotationHeight = delegate.collectionView(
collectionView: collectionView,
heightForAnnotationAtIndexPath: indexPath,
withWidth: cellWidth
)
let cellHeight = cellPadding + imageHeight + annotationHeight + cellPadding
let frame = CGRect(
x: xOffsets[column],
y: yOffsets[column],
width: collumnWidth,
height: cellHeight
)
let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)
let attributes = PinterestLayoutAttributes(
forCellWith: indexPath
)
attributes.frame = insetFrame
attributes.imageHeight = imageHeight
cache.append(attributes)
contentHeight = max(contentHeight, frame.maxY)
yOffsets[column] = yOffsets[column] + cellHeight
}
if let footerSize = delegate.collectionView?(
collectionView: collectionView,
sizeForSectionFooterViewForSection: section
) {
let footerX = (contentWidth - footerSize.width) / 2
let footerFrame = CGRect(
origin: CGPoint(
x: footerX,
y: contentHeight
),
size: footerSize
)
let footerAttributes = PinterestLayoutAttributes(
forSupplementaryViewOfKind: UICollectionElementKindSectionFooter,
with: IndexPath(item: 0, section: section)
)
footerAttributes.frame = footerFrame
cache.append(footerAttributes)
contentHeight = footerFrame.maxY
}
}
}
}
override public func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attributes in cache {
if attributes.frame.intersects(rect) {
layoutAttributes.append(attributes)
}
}
return layoutAttributes
}
}