自定义UICollectionView布局的编码

3

我应该在哪里/如何编写代码以实现此单元格布局行为。

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

第二个单元格的 centerX 为第一个单元格的右侧边缘,以此类推...

enter image description here

我应该在自定义 UICollectionViewLayout 中重写哪个方法?


我猜你在寻找这个:https://dev59.com/uZXfa4cB1Zd3GeqPi7SS - Zohaib Hassan
1个回答

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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接