我有一个名为 timeline 的 collectionView,它在此处的倒数第二行以编程方式滚动:
internal func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if !onceOnly {
let indexToScrollTo = IndexPath(row: self.posts.count - 1, section: 0)
collectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
let firstPost = posts.first?.timeStamp
let firstOfFirstMonth = firstPost?.startOfMonth()
let diff = posts.last?.timeStamp.months(from: firstOfFirstMonth!)
//self.currentPostMonth = diff
let monthCellIndexPath = IndexPath(row: diff!, section: 0)
timeline.scrollToItem(at: monthCellIndexPath, at: .centeredHorizontally, animated: false)
onceOnly = true
}
}
稍后..我正在尝试检测这已经完成滚动
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView == timeline {
print("Did finish")
}
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
if scrollView == timeline {
print("Did finish")
}
}
滚动完成后,两个打印语句都不会触发。我认为部分是因为 animation = false。当我将其设置为 true 时,它打印 Did finish correctly - 我认为特别是 scrollViewDidEndScrollingAnimation 打印,即使 scrollViewDidEndDecelerating 仍然不执行任何操作,因为它以编程方式滚动。
我怎样才能检测到这个卷轴已经完成了?
最佳答案
我认为是因为您将 collectionView 与 scrollView 进行了比较
if scrollView == timeline
这是 false
,顺便说一句,你可以使用
func scrollViewDidScroll(_ scrollView: UIScrollView)
如果没有动画那么在之后的下一行
timeline.scrollToItem(at: monthCellIndexPath, at: .centeredHorizontally, animated: false)
运行你想要的,因为它会在滚动成功结束的地方连续发生,无论是否调用,都不需要在 scrollView 的委托(delegate)方法中设置它
关于ios - 当动画为假时,如何检测 UIScrollView 已完成滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52174319/