【问题标题】:How to determine when a custom UICollectionViewCell is 100% on the screen如何确定自定义 UICollectionViewCell 何时在屏幕上显示为 100%
【发布时间】:2017-10-19 12:29:23
【问题描述】:

从上图中,我有 UICollectionView 和 4 个自定义单元格。任何时候屏幕上都可以显示 2 或 3 个单元格。如何判断“单元格 1”或“单元格 2”何时 100% 显示在屏幕上?

两个

collectionView.visibleCells
collectionView.indexPathsForVisibleItems

返回数组并且不告诉你屏幕上的单元格是否 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>>]

collectionView.indexPathsForVisibleItems

[[0, 1], [0, 0], [0, 2]] 

【问题讨论】:

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

这将为完全可见的单元格返回一个 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)

}

【讨论】:

  • 这应该在哪里调用?一直按按钮对我来说似乎很荒谬。我的意思是,一旦我的滚动停止,我有什么办法可以知道。参考:stackoverflow.com/q/53055965/4306200
  • 感谢您的解决方案
  • 为什么是 collectionView.superview 而不仅仅是 collectionView?
  • @LichiParejoAlcazar - 如果将单元格框架转换为collectionView,则生成的origin.x 将与集合视图的内容相关。因此,如果您的单元格为 100 分宽,并且您已滚动以使第 10 个单元格的左边缘位于视图的左边缘,cellRect.origin.x 将是900(9 个单元格在屏幕外)。 .. 使用collectionView.superview 将其相对于超级视图进行转换,因此cellRect.origin.x 将是0。自己试试...添加一些print() 语句以查看结果。
【解决方案2】:

这是它的扩展,基于唐 DonMag 的回答:

extension UICollectionView {
    var fullyVisibleCells : [UICollectionViewCell] {
        return self.visibleCells.filter { cell in
            let cellRect = self.convert(cell.frame, to: self.superview)
            return self.frame.contains(cellRect) }
    }
}

【讨论】:

    【解决方案3】:

    您可以过滤您的 visibleCells 数组以检查您的单元格的框架是否包含在您的 collectionView 的框架中:

        var visibleCells = self.collectionView?.visibleCells
        visibleCells = visibleCells?.filter({ cell -> Bool in
            return self.collectionView?.frame.contains(cell.frame) ?? false
        })
        print (visibleCells)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 2023-03-16
      • 2022-07-22
      • 2016-05-14
      • 2018-10-21
      • 1970-01-01
      • 2019-12-07
      相关资源
      最近更新 更多