编码自定义uicollectionview布局

时间:2017-07-06 19:40:55

标签: ios uicollectionview uicollectionviewlayout

我在哪里/如何编码此单元格布局行为。

我需要每个单元格与之前的单元格重叠。

Cell 2 centerX是Cell 1右边缘等等......

enter image description here

我会在自定义UICollectionViewLayout中覆盖哪种方法?

1 个答案:

答案 0 :(得分:2)

创建自定义collectionView布局时,覆盖layoutAttributesForItem以配置单元格布局行为...

var preferredSize: CGSize? = CGSize(width: 100, height: 100)


override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)

    attributes.size = preferredSize!
//Modifying the centerX property adjust the overlapping effect
    let centerX = (preferredSize?.width)! / 2.0 + CGFloat(indexPath.item) * ((preferredSize?.width)! * 0.5 )
    let centerY = collectionView!.bounds.height / 2.0
    attributes.center = CGPoint(x: centerX, y: centerY)
    attributes.zIndex = -(indexPath.item)

    return attributes
}