如何确定自定义UICollectionViewCell何时在屏幕上100%

Pau*_* S. 10 ios uicollectionview uicollectionviewcell swift

在此输入图像描述

从上图我有UICollectionView4个自定义单元格.任何时候屏幕上都可以有2或3个单元格.如何判断屏幕上"单元格1"或"单元格2"何时为100%?

collectionView.visibleCells
collectionView.indexPathsForVisibleItems
Run Code Online (Sandbox Code Playgroud)

返回数组并不会告诉你屏幕上是否有100%的单元格.

在图像的情况下,将显示以下内容 didSelectItemAt

collectionView.visibleCells

[<Shot_On_Goal.MainCollectionViewCell: 0x101f525c0; baseClass = UICollectionViewCell; frame = (190 7.66667; 454 350); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x1c0237300>>, <Shot_On_Goal.HeaderCollectionViewCell: 0x101f4d580; baseClass = UICollectionViewCell; frame = (10 0; 170 365); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x1c0236800>>, <Shot_On_Goal.TheirHockeyNetCollectionViewCell: 0x101f55520; baseClass = UICollectionViewCell; frame = (654 7.66667; 454 350); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x1c0238fe0>>]
Run Code Online (Sandbox Code Playgroud)

collectionView.indexPathsForVisibleItems

[[0, 1], [0, 0], [0, 2]] 
Run Code Online (Sandbox Code Playgroud)

Don*_*Mag 12

这将返回完全可见单元格的IndexPaths数组:

func fullyVisibleCells(_ inCollectionView: UICollectionView) -> [IndexPath] {

    var returnCells = [IndexPath]()

    var vCells = inCollectionView.visibleCells
    vCells = vCells.filter({ cell -> Bool in
        let cellRect = inCollectionView.convert(cell.frame, to: inCollectionView.superview)
        return inCollectionView.frame.contains(cellRect) 
    })

    vCells.forEach({
        if let pth = inCollectionView.indexPath(for: $0) {
            returnCells.append(pth)
        }
    })

    return returnCells

}

@IBAction func test(_ sender: Any) {

    let visCells = fullyVisibleCells(self.collectionView)
    print(visCells)

}
Run Code Online (Sandbox Code Playgroud)