我有一个与 collectionView 大小相同的垂直方向单元格,我设置了 collectionView.isPagingEnabled = true
。一次屏幕上只有 1 个事件的可见单元格:
layout.scrollDirection = .vertical
collectionView.isPagingEnabled = true
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// collectionView is the same width and height as the device
let width = collectionView.frame.width
let height = collectionView.frame.height
return CGSize(width: width, height: height)
}
我需要知道在用户上下滑动后整个单元格何时显示在屏幕上(当页面完成分页并在屏幕上居中时)。或者换句话说,我需要知道用户何时没有在单元格之间滑动或拖动。我试过这个:
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if decelerate {
if scrollView.frame.height == collectionView.frame.height {
let indexPaths = collectionView.indexPathsForVisibleItems
for indexPath in indexPaths {
if let cell = collectionView.cellForItem(at: indexPath) as? MyCell {
print("currentCell: \(indexPath.item)") // do something in cell
}
}
}
}
}
我遇到的问题是即使我以 1/2 的方式拖动,这意味着如果我在项目 2 上,将 1/2 拖动到项目 3,然后再拖回项目 2,print("currentCell :\(indexPath.item)")
将打印出 currentCell: 3
然后是 currentCell: 2
。唯一应该打印 currentCell: 3
的情况是该单元格完全在屏幕上,而不是在单元格 2 和 3(或 2 和 1 等)之间。
最佳答案
你可以尝试这样的事情:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let y = targetContentOffset.pointee.y
let item = Int(y / view.frame.height)
print("current cell: \(item)")
}
在这段代码中,我使用框架的宽度来划分 x,因为我的 collectionView 是水平的,您可以使用高度作为垂直轴。希望对你有帮助
关于ios - 使用 collectionView.isPagingEnabled = true 如何知道分页何时完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62549413/