禁用 UIScrollView 中子视图的滚动

发布于 2025-01-10 02:53:27 字数 94 浏览 5 评论 0原文

我在滚动视图的顶部有一个视频播放器视图,并且添加了垂直滑动来调整音量和亮度功能。

问题是当我在播放器视图中滑动来调整音量时,滚动视图也会滚动,我该如何防止它呢?

I have a video player view on the top of the scroll view, and I added swipe vertically to adjust volume and brightness feature.

The problem is when I swipe in the player view to adjust the volume, the scroll view scrolls as well, how can I prevent it?

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

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

发布评论

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

评论(1

谁把谁当真 2025-01-17 02:53:27

这可能适合您的用例。

我假设您在视频视图上添加了平移手势,因此在操作处理程序中,您可以在手势状态为 .began时禁用滚动视图滚动>.changed 并为任何状态重新启用它,例如 endcancelled

以下是我的做法:

@objc
func didPan(_ panGesture: UIGestureRecognizer)
{
    scrollView.isScrollEnabled
        = panGesture.state != .began && panGesture.state != .changed
    
    /* The above is the same as these lines of code
    if panGesture.state == .began || panGesture.state == .ended
    {
        scrollView.isScrollEnabled = false
        return
    }
    
    scrollView.isScrollEnabled = true
    */
}

以下是发生的情况的示例这段代码:

  1. 当我平移视频时,它会显示标签并阻止滚动视图移动
  2. 当我平移滚动视图内但视频区域外(黄色区域)的另一个视图时,它处于活动状态

UIView ios Swift 上 UIScrollView 平移手势中的 AVPlayer

This could probably work for your use case.

I am assuming you added a pan gesture on your video view and so in your action handler, you could disable the scrollview from scrolling when the gesture's state is .began or .changed and re-enable it for any any state such as ended or cancelled

Here is how I did that:

@objc
func didPan(_ panGesture: UIGestureRecognizer)
{
    scrollView.isScrollEnabled
        = panGesture.state != .began && panGesture.state != .changed
    
    /* The above is the same as these lines of code
    if panGesture.state == .began || panGesture.state == .ended
    {
        scrollView.isScrollEnabled = false
        return
    }
    
    scrollView.isScrollEnabled = true
    */
}

Here is an example of what happens with this code:

  1. When I pan on the video, it shows a label and stops the scroll view from moving
  2. When I pan on another view inside the scrollview but outside the video region (in the yellow region), it is active

AVPlayer in UIScrollView pan gesture on UIView ioS Swift

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