Skip to content

Commit 32ba434

Browse files
committed
refactored for requested functionality
1 parent 2ca7029 commit 32ba434

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

LUX/Classes/Base/CollectionViews/LUXPinterestStyleLayout.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class LUXPinterestStyleLayout: UICollectionViewLayout {
2020
private var _baseItemWidth: CGFloat? { didSet { invalidateLayout() }}
2121
public func setBaseItemWidth(_ width: CGFloat) { _baseItemWidth = width }
2222

23-
public var setWidthForColumn: Int?
24-
private var widthForColumn: ((Int) -> CGFloat?)? = { _ in return nil }
23+
public var setWidthCalculatorForColumn: CGFloat?
24+
public var calculateMultiWidthColumnForColumn: ((_ multiWidth: CGFloat...) -> [CGFloat?])?
2525

2626
private var contentHeight: CGFloat = 0.0
2727
private var contentWidth: CGFloat {
@@ -72,18 +72,28 @@ public class LUXPinterestStyleLayout: UICollectionViewLayout {
7272
var offSet: CGFloat = 0
7373

7474
if let columnCount = _columnCount {
75-
let columnWidth = contentWidth / CGFloat(_columnCount!)
75+
let columnWidth = contentWidth / CGFloat(columnCount)
7676
offSet = columnWidth
77+
7778
for column in 0..<columnCount {
78-
let width = (widthForColumn ?? { _ in return 0 })(setWidthForColumn ?? 0) ?? offSet
79-
xOffSet.append(width * CGFloat(column))
79+
80+
if let multiWidth = calculateMultiWidthColumnForColumn {
81+
if let setWidth = setWidthCalculatorForColumn {
82+
for ( _, size) in multiWidth(setWidth).enumerated() {
83+
xOffSet.append(size! * CGFloat(column))
84+
}
85+
}
86+
}
87+
xOffSet.append(offSet * CGFloat(column))
8088
}
8189
} else if let baseWidth = _baseItemWidth {
8290
if _columnCount == nil {
8391
columnCount = Int(contentWidth / baseWidth)
8492
offSet = baseWidth / CGFloat(columnCount)
8593

8694
for column in 0..<columnCount {
95+
96+
8797
xOffSet.append(offSet * CGFloat(column))
8898
}
8999
} else {
@@ -109,7 +119,7 @@ public class LUXPinterestStyleLayout: UICollectionViewLayout {
109119
var column = 0
110120
var yOffset: [CGFloat] = .init(repeating: 0, count: _columnCount!)
111121

112-
for item in 0..<itemCount {
122+
for item in 0..<itemCount {
113123
let indexPath = IndexPath(item: item, section: 0)
114124

115125
let photoHeight = heightForCellAtIndexPath?(collectionView, indexPath) ?? 180
@@ -125,11 +135,9 @@ public class LUXPinterestStyleLayout: UICollectionViewLayout {
125135
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
126136
attributes.frame = insetFrame
127137
cache.append(attributes)
128-
129138
contentHeight = max(contentHeight, frame.maxY)
130139
yOffset[column] = yOffset[column] + height
131-
132-
column = column < ( _columnCount! - 1) ? (column + 1) : 0
140+
column = column < ( _columnCount! - 1) ? (column + 1) : 0
133141
}
134142
}
135143

0 commit comments

Comments
 (0)