I am using autoScroll on an orthogonal section of the collectionView using compositional layout. I need to invalidate the autoscroll timer as soon as the user manually scrolls the section.
I could use scrollViewDidBeginDragging
/ scrollViewWillBeginDecelerating
, but the scrollView delegates never get called on orthogonal sections.
If anyone has any workaround to detect user scroll event in this case, it will be helpful. Thank you.
Detect userEvent/pan/scroll on orthogonal sections in UICollectionViewCompositionalLayout
After trying out several solutions, I found the best and the simplest solution.
I added a UIPanGestureRecogniser to the UICollectionViewCell to listen to user pan events. In the selector, I just invalidate the timer. That's it!
Also we need to return true by overriding gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
so that the vertical scroll and horizontal scrolls works properly.
This is what I added to the UICollectionViewCell class:
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
}
}
With this, every time the user tries to scroll, I invalidate the autoScroll timer
© 2022 - 2024 — McMap. All rights reserved.