UICollectionViewLayout和方法layoutAttributesForItem永远不会被调用



我正在尝试实现UICollectionViewFlowLayoutUICollectionViewLayout -任何方式layoutAttributesForItem都不会被调用。

我可以从别人那里看到他们从self.layoutAttributesForItem调用layoutAttributesForItem

就像这个流出的例子:

我已经创建了一个Playground项目,你可以看看。总的来说,我只是想放大中心视图

尝试使用子类化UICollectionViewFlowLayout

let flowLayout = LeftAgignedFlowLayout()
flowLayout.minimumLineSpacing = 18
flowLayout.minimumInteritemSpacing = 8
flowLayout.scrollDirection = .vertical
self.collectionViewLayout = flowLayout

class LeftAgignedFlowLayout : UICollectionViewFlowLayout {        
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let arr = super.layoutAttributesForElements(in: rect)!
        return arr.map {
            atts in 
            var atts = atts
            if atts.representedElementCategory == .cell {
                let ip = atts.indexPath
                atts = self.layoutAttributesForItem(at:ip)!
            }
            return atts
        }
    }
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        var atts = super.layoutAttributesForItem(at:indexPath)!
        if indexPath.item == 0 {
            return atts 
        }
        if atts.frame.origin.x - 1 <= self.sectionInset.left {
            return atts 
        }
        let ipPv = IndexPath(item:indexPath.row-1, section:indexPath.section)
        let fPv = self.layoutAttributesForItem(at:ipPv)!.frame
        let rightPv = fPv.origin.x + fPv.size.width + self.minimumInteritemSpacing
        atts = atts.copy() as! UICollectionViewLayoutAttributes
        atts.frame.origin.x = rightPv
        return atts
    }
}

相关内容

  • 没有找到相关文章

最新更新