我有一个称为时间轴的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")
}
}
滚动完成后,两个print语句均不会触发。我认为部分原因是animation = false。当我将其设置为true时,它会打印“是否正确完成”-我认为特别是scrollViewDidEndScrollingAnimation会打印,即使scrollViewDidEndDecelerating仍然无法执行任何操作,因为它是通过编程方式滚动的。
我如何检测到滚动结束?
答案 0 :(得分:1)
我认为是因为您将collectionView与scrollView进行了比较
if scrollView == timeline
false
,顺便说一句
func scrollViewDidScroll(_ scrollView: UIScrollView)
如果没有动画,则在下一行之后
timeline.scrollToItem(at: monthCellIndexPath, at: .centeredHorizontally, animated: false)
运行所需的内容,因为它将连续发生在滚动成功结束的地方,而无需在scrollView的委托方法中设置它(无论是否调用)