/
CenteredFlowLayout.swift
207 lines (162 loc) · 8.17 KB
/
CenteredFlowLayout.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// CenteredFlowLayout.swift
// CenteredFlowLayout
//
// Created by Nilson Souto on 6/15/16.
// Copyright © 2016 xissburg. All rights reserved.
//
import UIKit
@objc public protocol CenteredFlowLayoutDelegate: UICollectionViewDelegate {
func centeredFlowLayout(layout: CenteredFlowLayout, sizeAtIndexPath indexPath: NSIndexPath) -> CGSize
}
public class CenteredFlowLayout: UICollectionViewLayout {
@IBOutlet weak var delegate: AnyObject?//CenteredFlowLayoutDelegate?
var frames: Array<Array<CGRect>> = []
var contentSize: CGSize = CGSizeZero
@IBInspectable var cellSpacing: CGFloat = 5
@IBInspectable var lineSpacing: CGFloat = 7
@IBInspectable var sectionSpacing: CGFloat = 20
class func centeredRects(rects: Array<CGRect>, width: CGFloat, cellSpacing: CGFloat) -> Array<CGRect> {
var rectsWidth: CGFloat = 0
for r in rects {
rectsWidth += r.size.width
}
rectsWidth += CGFloat(rects.count - 1) * cellSpacing
let left = width/2 - rectsWidth/2
let offset = left - rects.first!.origin.x
var centeredRects = Array<CGRect>()
for r in rects {
let rect = CGRectMake(r.origin.x + offset, r.origin.y, r.size.width, r.size.height)
centeredRects.append(rect)
}
return centeredRects
}
public struct Measurements {
public var collectionViewSize: CGSize = CGSizeZero
public var collectionViewContentInset: UIEdgeInsets = UIEdgeInsetsZero
public var numberOfItemsInSections: [Int] = []
public var itemsSizes: [[CGSize]] = []
public var lineSpacing: CGFloat = 5
public var cellSpacing: CGFloat = 7
public var sectionSpacing: CGFloat = 20
public init() {}
}
class public func calculateFramesAndContentSizeWithMeasurements(measurements: Measurements) -> ([[CGRect]], CGSize) {
var frames: [[CGRect]] = []
var contentSize = CGSizeZero
let sections = measurements.numberOfItemsInSections.count
for section in 0 ..< sections {
let count = measurements.numberOfItemsInSections[section]
var sectionFrames = Array<CGRect>()
var lineFrames = Array<CGRect>()
var lineHeight: CGFloat = 0
var x: CGFloat = 0
var y: CGFloat = contentSize.height
let insets = measurements.collectionViewContentInset
let width = measurements.collectionViewSize.width - insets.left - insets.right
for i in 0 ..< count {
var size = measurements.itemsSizes[section][i]
size.width = min(measurements.collectionViewSize.width, size.width)
if size.height > lineHeight {
lineHeight = size.height
}
var frame = CGRectMake(x, y, size.width, size.height)
if frame.origin.x + frame.size.width > width {
x = 0
y += lineHeight + measurements.lineSpacing
contentSize.height += lineHeight + measurements.lineSpacing
if lineFrames.count > 0 {
sectionFrames += centeredRects(lineFrames, width: width, cellSpacing: measurements.cellSpacing)
lineFrames.removeAll(keepCapacity: true)
frame.origin.x = x
frame.origin.y = y
lineFrames.append(frame)
x += frame.size.width + measurements.cellSpacing
if i == count - 1 {
sectionFrames += centeredRects(lineFrames, width: width, cellSpacing: measurements.cellSpacing)
if section <= sections - 1 {
contentSize.height += lineHeight
}
}
}
else {
frame.origin.x = width/2 - frame.size.width/2
sectionFrames.append(frame)
}
lineHeight = 0
}
else if i == count - 1 {
lineFrames.append(frame)
sectionFrames += centeredRects(lineFrames, width: width, cellSpacing: measurements.cellSpacing)
contentSize.height += lineHeight + measurements.lineSpacing
}
else {
lineFrames.append(frame)
x += size.width + measurements.cellSpacing
}
}
assert(sectionFrames.count == count)
frames.append(sectionFrames)
let sectionWidth = measurements.collectionViewSize.width - insets.left - insets.right
contentSize.width = max(contentSize.width, sectionWidth)
if section < sections - 1 {
contentSize.height += measurements.sectionSpacing
}
}
return (frames, contentSize)
}
override public func prepareLayout() {
super.prepareLayout()
if let collectionView = collectionView, dataSource = collectionView.dataSource, delegate = delegate {
var measurements = Measurements()
measurements.collectionViewSize = collectionView.bounds.size
measurements.collectionViewContentInset = collectionView.contentInset
measurements.lineSpacing = lineSpacing
measurements.cellSpacing = cellSpacing
measurements.sectionSpacing = sectionSpacing
var numberOfItemsInSections: [Int] = []
var numberOfSections = 1
if let f = dataSource.numberOfSectionsInCollectionView {
numberOfSections = f(collectionView)
}
for i in 0..<numberOfSections {
numberOfItemsInSections.append(dataSource.collectionView(collectionView, numberOfItemsInSection: i))
}
measurements.numberOfItemsInSections = numberOfItemsInSections
var itemsSizes: [[CGSize]] = []
for i in 0..<numberOfSections {
var sizes: [CGSize] = []
for j in 0..<numberOfItemsInSections[i] {
let indexPath = NSIndexPath(forItem: j, inSection: i)
let size = delegate.centeredFlowLayout(self, sizeAtIndexPath: indexPath)
sizes.append(size)
}
itemsSizes.append(sizes)
}
measurements.itemsSizes = itemsSizes
(frames, contentSize) = CenteredFlowLayout.calculateFramesAndContentSizeWithMeasurements(measurements)
}
}
override public func collectionViewContentSize() -> CGSize {
return contentSize
}
override public func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attributes.frame = frames[indexPath.section][indexPath.item]
return attributes
}
override public func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var attributesArray = Array<UICollectionViewLayoutAttributes>()
for (section, sectionFrames) in frames.enumerate() {
for (index, frame) in sectionFrames.enumerate() {
if CGRectIntersectsRect(rect, frame) {
let indexPath = NSIndexPath(forItem: index, inSection: section)
let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attributes.frame = frame
attributesArray.append(attributes)
}
}
}
return attributesArray
}
}