Я хочу изменить порядок своих ячеек в моем наборе «Водопад» Но когда я двигаю свою камеру, размер меняется. Чтобы сделать макет, я использовал этот урок https://www.raywenderlich.com/164608/uicollectionview-custom-layout-tutorial-pinterest-2
Как я могу исправить размер моего предмета при изменении этой позиции?
My ViewLayout:
import Foundation
import UIKit
protocol WidgetCollectionViewLayoutDelegate: class {
func collectionView(_ collectionView: UICollectionView, heightForPhotoAt indexPath:IndexPath) -> CGFloat
}
class WidgetCollectionViewLayout: UICollectionViewLayout {
weak var delegate: WidgetCollectionViewLayoutDelegate!
private let numberOfColumns: Int = 2
private let cellPadding: CGFloat = 6
private var cache = [UICollectionViewLayoutAttributes]()
private var contentHeight: CGFloat = 0
private var contentWidth: CGFloat {
guard let collectionView = collectionView else {
return 0
}
let insets = collectionView.contentInset
return collectionView.bounds.width - (insets.left + insets.right)
}
override var collectionViewContentSize: CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}
override func prepare() {
print("prepare")
super.prepare()
guard cache.isEmpty == true, let collectionView = collectionView else {
return
}
calculContent()
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var visibleLayoutAttributes = [UICollectionViewLayoutAttributes]()
for attributes in cache {
if attributes.frame.intersects(rect) {
visibleLayoutAttributes.append(attributes)
}
}
return visibleLayoutAttributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return cache[indexPath.item]
}
private func calculContent() {
let columnWidth = contentWidth / CGFloat(numberOfColumns)
var xOffset = [CGFloat]()
for column in 0 ..< numberOfColumns {
xOffset.append(CGFloat(column) * columnWidth)
}
var column = 0
var yOffset = [CGFloat](repeating: 0, count: numberOfColumns)
for item in 0 ..< collectionView!.numberOfItems(inSection: 0) {
let indexPath = IndexPath(item: item, section: 0)
let photoHeight = delegate.collectionView(collectionView!, heightForPhotoAt: indexPath)
let height = cellPadding * 2 + photoHeight
let frame = CGRect(x: xOffset[column], y: yOffset[column], width: columnWidth, height: height)
let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = insetFrame
cache.append(attributes)
contentHeight = max(contentHeight, frame.maxY)
yOffset[column] = yOffset[column] + height
column = column < (numberOfColumns - 1) ? (column + 1) : 0
}
}
}
Заранее благодарю за помощь