保持单元格的大小 - 具有自定义布局的uicollectionview

时间:2018-05-02 08:17:00

标签: swift uicollectionview uicollectionviewlayout

我想在“瀑布”集合视图中重新排序我的单元格。但当我移动我的手机时,尺寸会发生变化。为了进行布局,我使用了本教程https://www.raywenderlich.com/164608/uicollectionview-custom-layout-tutorial-pinterest-2

如果此位置发生变化,如何修正项目的大小?

enter image description here

我的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
        }
    }
}

提前感谢您的帮助

0 个答案:

没有答案