iOS:我可以通过编程方式决定与哪个组件交互(滚动)吗?

iOS: can I programmatically decide what component to interact with (scrolling)?

我现在有两个可滚动视图,一个是水平视图,另一个是垂直视图。有没有一种方法可以检测用户的手势是垂直还是水平,并据此选择要与之交互的视图?

感谢您的帮助。

请查看 isDirectionalLockEnabled 是否有帮助。 这有助于锁定滚动方向。

If this property is YES and the user begins dragging in one general direction (horizontally or vertically), the scroll view disables scrolling in the other direction. If the drag direction is diagonal, then scrolling will not be locked and the user can drag in any direction until the drag completes.

您可以使用 UIScrollViewDelegate 中的 scrollViewWillBeginDragging 来获取滚动的方向。

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
    if translation.y > 0 {
        // swipes from top to bottom of screen -> down
    } else {
        // swipes from bottom to top of screen -> up
    }
}

但是由于此方法return 不是滚动视图,您将只知道拖动了哪个滚动条以及向哪个方向拖动。