我正在使用组合布局的 collectionView 的正交部分上使用 autoScroll。我需要在用户手动滚动该部分后立即使自动滚动计时器无效。 我可以使用scrollViewDidBeginDragging/scrollViewWillBeginDecelerating,但scrollView委托(delegate)永远不会在正交部分上被调用。 如果有人有任何解决方法来检测这种情况下的用户滚动事件,那将会很有帮助。谢谢。
最佳答案
在尝试了几种解决方案后,我找到了最好也是最简单的解决方案。
我向 UICollectionViewCell 添加了一个 UIPanGestureRecogniser 来监听用户平移事件。在选择器中,我只是使计时器无效。就是这样!
此外,我们需要通过重写 gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
返回 true,以便垂直滚动和水平滚动正常工作。
这是我添加到 UICollectionViewCell 类中的内容:
class CustomCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: .zero)
pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
pan.delegate = self
self.addGestureRecognizer(pan)
}
@objc private func handlePan(_ pan: UIPanGestureRecognizer) {
delegate?.invalidateTimer()
}
}
extension CustomCell: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
这样,每次用户尝试滚动时,我都会使 autoScroll 计时器失效
关于swift - 检测 UICollectionViewCompositionalLayout 中正交部分上的 userEvent/pan/scroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72038786/