检测UicollectionViewCompositionallayout中正交部分上的userefent/pan/scroll

发布于 2025-01-24 13:27:50 字数 231 浏览 0 评论 0原文

我使用构图布局在CollectionView的正交部分上使用自动滚动。我需要在用户手动滚动该部分后立即使自动滚动计时器无效。 我可以使用scrollviewDidbeGindRagging/scrollViewWillBegIndeCelerating,但是ScrollView委托永远不会在正交部分上调用。 如果在这种情况下有人有任何解决方法可以检测用户滚动事件,那将是有帮助的。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2025-01-31 13:27:50

尝试了几种解决方案后,我找到了最好和最简单的解决方案。
我在UicollectionViewCell中添加了一个UipangeSturerecogniser,以收听用户PAN事件。在选择器中,我只是使计时器无效。就是这样!
另外,我们需要通过覆盖gesturercognizer(_:syseReRecognizeSimultanealywith:)来返回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
    }
}

这样,每次用户尝试滚动时,我都会使自动滚动计时器无效

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文