如何确定自定义 UICollectionViewCell 何时在屏幕上显示为 100%
How to determine when a custom UICollectionViewCell is 100% on the screen
从上图中我有 UICollectionView
和 4 个自定义单元格。屏幕上可以随时显示 2 或 3 个单元格。我如何判断 "cell 1" 或 "cell 2" 何时在屏幕上显示为 100%?
两者都
collectionView.visibleCells
collectionView.indexPathsForVisibleItems
return 数组,但不会告诉您哪个单元格 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]]
您可以过滤 visibleCells 数组以检查单元格的框架是否包含在 collectionView 的框架中:
var visibleCells = self.collectionView?.visibleCells
visibleCells = visibleCells?.filter({ cell -> Bool in
return self.collectionView?.frame.contains(cell.frame) ?? false
})
print (visibleCells)
这将为完全可见的单元格 return 一个索引路径数组:
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: [=10=]) {
returnCells.append(pth)
}
})
return returnCells
}
@IBAction func test(_ sender: Any) {
let visCells = fullyVisibleCells(self.collectionView)
print(visCells)
}
这是它的扩展,基于 don 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) }
}
}
从上图中我有 UICollectionView
和 4 个自定义单元格。屏幕上可以随时显示 2 或 3 个单元格。我如何判断 "cell 1" 或 "cell 2" 何时在屏幕上显示为 100%?
两者都
collectionView.visibleCells
collectionView.indexPathsForVisibleItems
return 数组,但不会告诉您哪个单元格 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]]
您可以过滤 visibleCells 数组以检查单元格的框架是否包含在 collectionView 的框架中:
var visibleCells = self.collectionView?.visibleCells
visibleCells = visibleCells?.filter({ cell -> Bool in
return self.collectionView?.frame.contains(cell.frame) ?? false
})
print (visibleCells)
这将为完全可见的单元格 return 一个索引路径数组:
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: [=10=]) {
returnCells.append(pth)
}
})
return returnCells
}
@IBAction func test(_ sender: Any) {
let visCells = fullyVisibleCells(self.collectionView)
print(visCells)
}
这是它的扩展,基于 don 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) }
}
}