自定义 uicollectionview 布局编码

coding for custom uicollectionview layout

Where/How 我会为此单元格布局行为编写代码吗?

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

单元格 2 centerX 是单元格 1 的右边缘等等...

我会在我的自定义 UICollectionViewLayout 中覆盖什么方法?

创建自定义 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
}