1

我正在探索 IGListKit 并尝试使用 IGListSectionController 子类来实现这一点:

在此处输入图像描述

基本上,每个彩色区域都是一个 UICollectionViewCell。我已经做了一些没有绿色区域的事情。但是,我想要的是上面示例中的布局。我该怎么做呢?

4

1 回答 1

2

我找到了答案,它实际上非常简单。因此,(就像在 UICollectionView 中一样)它必须使用自定义 UICollectionViewLayout 进行设置。所以我有这样的事情:

let collectionView:IGListCollectionView = {
    let layout = MyCustomLayout()
    let view = IGListCollectionView(frame: .zero, collectionViewLayout: layout)
    return view
}() 

这是我的自定义布局的样子:

protocol MyCustomLayoutDelegate {
    func heightForItem(inCollectionView: UICollectionView, at position: Int) -> CGFloat
}

class MyCustomLayout: UICollectionViewLayout, MyCustomLayoutDelegate {

    var delegate:MyCustomLayoutDelegate?

    private var cache = [UICollectionViewLayoutAttributes]()

    private var aHeight:CGFloat = 0

    private var contentWidth:CGFloat {
        return collectionView!.bounds.width
    }

    override func prepare() {

        if cache.isEmpty {

            var yOffset:CGFloat = 0

            for section in 0 ..< collectionView!.numberOfSections {
                let contentHeight = (delegate ?? self).heightForItem(inCollectionView: collectionView!, at: section)
                let leftAreaWidth = contentHeight - 1
                let topAreaHeight:CGFloat = 20.0
                let bottomAreaHeight = contentHeight - topAreaHeight - 1
                let bottomAreaWidth = contentWidth - leftAreaWidth

                let leftAreaIndexPath = IndexPath(item: 0, section: section)
                let topAreaIndexPath = IndexPath(item: 1, section: section)
                let bottomAreaIndexPath = IndexPath(item: 2, section: section)

                let leftAreaFrame = CGRect(x: 0, y: yOffset, width: leftAreaWidth, height: leftAreaWidth)
                let topAreaFrame = CGRect(x: leftAreaWidth, y: yOffset, width: bottomAreaWidth, height: topAreaHeight)
                let bottomAreaFrame = CGRect(x: leftAreaWidth, y: yOffset + topAreaHeight, width: bottomAreaWidth, height: bottomAreaHeight)

                let leftAreaAttributes = UICollectionViewLayoutAttributes(forCellWith: leftAreaIndexPath)
            leftAreaAttributes.frame = leftAreaFrame
            cache.append(leftAreaAttributes)

                let topAreaAttributes = UICollectionViewLayoutAttributes(forCellWith: topAreaIndexPath)
            topAreaAttributes.frame = topAreaFrame
            cache.append(topAreaAttributes)

                let bottomAreaAttributes = UICollectionViewLayoutAttributes(forCellWith: bottomAreaIndexPath)
            bottomAreaAttributes.frame = bottomAreaFrame
            cache.append(bottomAreaAttributes)

                yOffset += contentHeight
            }

            aHeight = yOffset
        }
    }

    override var collectionViewContentSize: CGSize {
        return CGSize(width: contentWidth, height: aHeight)
    }

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        return cache.filter {
            $0.frame.intersects(rect)
        }
    }

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

        return cache.first {
            $0.indexPath == indexPath
        }

    }

    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {

        if let oldWidth = collectionView?.bounds.width {
            return oldWidth != newBounds.width
        }

        return false
    }

    override func invalidateLayout() {
        super.invalidateLayout()

        cache = []
        aHeight = 0
    }

    //Delegate
    internal func heightForItem(inCollectionView: UICollectionView, at postion: Int) -> CGFloat {
        return 0
    }
}
于 2017-04-25T10:33:03.523 回答