forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMosaicCollectionViewLayout.swift
More file actions
executable file
·259 lines (219 loc) · 9.32 KB
/
Copy pathMosaicCollectionViewLayout.swift
File metadata and controls
executable file
·259 lines (219 loc) · 9.32 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
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
249
250
251
252
253
254
255
256
257
258
259
//
// MosaicCollectionViewLayout
// Sample
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
// Modifications to this file made after 4/13/2017 are: Copyright (c) 2017-present,
// Pinterest, Inc. Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UIKit
import AsyncDisplayKit
protocol MosaicCollectionViewLayoutDelegate: ASCollectionDelegate {
func collectionView(_ collectionView: UICollectionView, layout: MosaicCollectionViewLayout, originalItemSizeAtIndexPath: IndexPath) -> CGSize
}
class MosaicCollectionViewLayout: UICollectionViewFlowLayout {
var numberOfColumns: Int
var columnSpacing: CGFloat
var _sectionInset: UIEdgeInsets
var interItemSpacing: UIEdgeInsets
var headerHeight: CGFloat
var _columnHeights: [[CGFloat]]?
var _itemAttributes = [[UICollectionViewLayoutAttributes]]()
var _headerAttributes = [UICollectionViewLayoutAttributes]()
var _allAttributes = [UICollectionViewLayoutAttributes]()
required override init() {
self.numberOfColumns = 2
self.columnSpacing = 10.0
self.headerHeight = 44.0 //viewcontroller
self._sectionInset = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)
self.interItemSpacing = UIEdgeInsetsMake(10.0, 0, 10.0, 0)
super.init()
self.scrollDirection = .vertical
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public var delegate : MosaicCollectionViewLayoutDelegate?
override func prepare() {
super.prepare()
guard let collectionView = self.collectionView else { return }
_itemAttributes = []
_allAttributes = []
_headerAttributes = []
_columnHeights = []
var top: CGFloat = 0
let numberOfSections: NSInteger = collectionView.numberOfSections
for section in 0 ..< numberOfSections {
let numberOfItems = collectionView.numberOfItems(inSection: section)
top += _sectionInset.top
if (headerHeight > 0) {
let headerSize: CGSize = self._headerSizeForSection(section: section)
let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, with: NSIndexPath(row: 0, section: section) as IndexPath)
attributes.frame = CGRect(x: _sectionInset.left, y: top, width: headerSize.width, height: headerSize.height)
_headerAttributes.append(attributes)
_allAttributes.append(attributes)
top = attributes.frame.maxY
}
_columnHeights?.append([]) //Adding new Section
for _ in 0 ..< self.numberOfColumns {
self._columnHeights?[section].append(top)
}
let columnWidth = self._columnWidthForSection(section: section)
_itemAttributes.append([])
for idx in 0 ..< numberOfItems {
let columnIndex: Int = self._shortestColumnIndexInSection(section: section)
let indexPath = IndexPath(item: idx, section: section)
let itemSize = self._itemSizeAtIndexPath(indexPath: indexPath);
let xOffset = _sectionInset.left + (columnWidth + columnSpacing) * CGFloat(columnIndex)
let yOffset = _columnHeights![section][columnIndex]
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = CGRect(x: xOffset, y: yOffset, width: itemSize.width, height: itemSize.height)
_columnHeights?[section][columnIndex] = attributes.frame.maxY + interItemSpacing.bottom
_itemAttributes[section].append(attributes)
_allAttributes.append(attributes)
}
let columnIndex: Int = self._tallestColumnIndexInSection(section: section)
top = (_columnHeights?[section][columnIndex])! - interItemSpacing.bottom + _sectionInset.bottom
for idx in 0 ..< _columnHeights![section].count {
_columnHeights![section][idx] = top
}
}
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
{
var includedAttributes: [UICollectionViewLayoutAttributes] = []
// Slow search for small batches
for attribute in _allAttributes {
if (attribute.frame.intersects(rect)) {
includedAttributes.append(attribute)
}
}
return includedAttributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
{
guard indexPath.section < _itemAttributes.count,
indexPath.item < _itemAttributes[indexPath.section].count
else {
return nil
}
return _itemAttributes[indexPath.section][indexPath.item]
}
override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
{
if (elementKind == UICollectionElementKindSectionHeader) {
return _headerAttributes[indexPath.section]
}
return nil
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
if (!(self.collectionView?.bounds.size.equalTo(newBounds.size))!) {
return true;
}
return false;
}
func _widthForSection (section: Int) -> CGFloat
{
return self.collectionView!.bounds.size.width - _sectionInset.left - _sectionInset.right;
}
func _columnWidthForSection(section: Int) -> CGFloat
{
return (self._widthForSection(section: section) - ((CGFloat(numberOfColumns - 1)) * columnSpacing)) / CGFloat(numberOfColumns)
}
func _itemSizeAtIndexPath(indexPath: IndexPath) -> CGSize
{
var size = CGSize(width: self._columnWidthForSection(section: indexPath.section), height: 0)
let originalSize = self.delegate!.collectionView(self.collectionView!, layout:self, originalItemSizeAtIndexPath:indexPath)
if (originalSize.height > 0 && originalSize.width > 0) {
size.height = originalSize.height / originalSize.width * size.width
}
return size
}
func _headerSizeForSection(section: Int) -> CGSize
{
return CGSize(width: self._widthForSection(section: section), height: headerHeight)
}
override var collectionViewContentSize: CGSize
{
var height: CGFloat = 0
if ((_columnHeights?.count)! > 0) {
if (_columnHeights?[(_columnHeights?.count)!-1].count)! > 0 {
height = (_columnHeights?[(_columnHeights?.count)!-1][0])!
}
}
return CGSize(width: self.collectionView!.bounds.size.width, height: height)
}
func _tallestColumnIndexInSection(section: Int) -> Int
{
var index: Int = 0;
var tallestHeight: CGFloat = 0;
_ = _columnHeights?[section].enumerated().map { (idx,height) in
if (height > tallestHeight) {
index = idx;
tallestHeight = height
}
}
return index
}
func _shortestColumnIndexInSection(section: Int) -> Int
{
var index: Int = 0;
var shortestHeight: CGFloat = CGFloat.greatestFiniteMagnitude
_ = _columnHeights?[section].enumerated().map { (idx,height) in
if (height < shortestHeight) {
index = idx;
shortestHeight = height
}
}
return index
}
}
class MosaicCollectionViewLayoutInspector: NSObject, ASCollectionViewLayoutInspecting
{
func collectionView(_ collectionView: ASCollectionView, constrainedSizeForNodeAt indexPath: IndexPath) -> ASSizeRange {
let layout = collectionView.collectionViewLayout as! MosaicCollectionViewLayout
return ASSizeRangeMake(CGSize.zero, layout._itemSizeAtIndexPath(indexPath: indexPath))
}
func collectionView(_ collectionView: ASCollectionView, constrainedSizeForSupplementaryNodeOfKind: String, at atIndexPath: IndexPath) -> ASSizeRange
{
let layout = collectionView.collectionViewLayout as! MosaicCollectionViewLayout
return ASSizeRange.init(min: CGSize.zero, max: layout._headerSizeForSection(section: atIndexPath.section))
}
/**
* Asks the inspector for the number of supplementary sections in the collection view for the given kind.
*/
func collectionView(_ collectionView: ASCollectionView, numberOfSectionsForSupplementaryNodeOfKind kind: String) -> UInt {
if (kind == UICollectionElementKindSectionHeader) {
return UInt((collectionView.dataSource?.numberOfSections!(in: collectionView))!)
} else {
return 0
}
}
/**
* Asks the inspector for the number of supplementary views for the given kind in the specified section.
*/
func collectionView(_ collectionView: ASCollectionView, supplementaryNodesOfKind kind: String, inSection section: UInt) -> UInt {
if (kind == UICollectionElementKindSectionHeader) {
return 1
} else {
return 0
}
}
func scrollableDirections() -> ASScrollDirection {
return ASScrollDirectionVerticalDirections;
}
}