【发布时间】:2021-03-15 00:18:13
【问题描述】:
我有一个启用分页的 collectionView:
collectionView.isPagingEnabled = true
collectionView 是全屏的(一次只显示 1 个单元格),我有一些在 scrollViewWillEndDragging 中运行的代码。当我正常滚动或半快速滚动时,cellForItem 和 scrollViewWillEndDragging 在同一页面/indexPath.item 上。但是当我快速滚动时,它们都搞砸了,cellForItem 总是在当前页面的后面
**当启用分页时,如何防止用户快速滚动?
var currentItem: Int?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeue...
currentItem = indexPath.item
return cell
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let yAxis = targetContentOffset.pointee.y
let page = Int(yAxis / collectionView.frame.height)
guard let currentItem = currentItem else { return }
if page == currentItem {
// only do something when page and the current indexPath.item are equal which works fine with normal scrolling
}
}
【问题讨论】:
标签: ios swift uicollectionview uiscrollview