在 SwiftUI 中向 ScrollView 内的视图添加拖动手势会阻止滚动 [英] Adding a drag gesture in SwiftUI to a View inside a ScrollView blocks the scrolling

查看:30
本文介绍了在 SwiftUI 中向 ScrollView 内的视图添加拖动手势会阻止滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 ScrollView 持有一组视图:

So I have a ScrollView holding a set of views:

    ScrollView {
        ForEach(cities) { city in
            NavigationLink(destination: ...) {
                CityRow(city: city)
            }
            .buttonStyle(BackgroundButtonStyle())
        }
    }

在每个视图中我都有一个拖动手势:

In every view I have a drag gesture:

    let drag = DragGesture()
        .updating($gestureState) { value, gestureState, _ in
            // ...
        }
        .onEnded { value in
            // ...
        }

我分配给视图的一部分:

Which I assign to a part of the view:

    ZStack(alignment: .leading) {
        HStack {
            // ...
        }
        HStack {
            // ...
        }
        .gesture(drag)
    }

一旦我附加了手势,ScrollView 就会停止滚动.让它滚动的唯一方法是从没有附加手势的部分开始滚动.我怎样才能避免它并使两者一起工作.在 UIKit 中,就像在 shouldRecognizeSimultaneousWith 方法中指定 true 一样简单.我如何在 SwiftUI 中拥有相同的内容?

As soon as I attach the gesture, the ScrollView stop scrolling. The only way to make it scroll it to start scrolling from a part of it which has no gesture attached. How can I avoid it and make both work together. In UIKit is was as simple as specifying true in shouldRecognizeSimultaneouslyWith method. How can I have the same in SwiftUI?

在 SwiftUI 中,我尝试使用 .simultaneousGesture(drag).highPriorityGesture(drag) 附加手势——它们的工作方式与 .gesture 相同(拖动).我还尝试为 include: 参数提供所有可能的静态 GestureMask 值——我要么滚动工作,要么我的拖动手势工作.永远不要两个.

In SwiftUI I've tried attaching a gesture using .simultaneousGesture(drag) and .highPriorityGesture(drag) – they all work the same as .gesture(drag). I've also tried providing all possible static GestureMask values for including: parameter – I have either scroll working or my drag gesture working. Never both of them.

这是我使用拖动手势的目的:

Here's what I'm using drag gesture for:

推荐答案

您可以将 minimumDistance 设置为某个值(例如 30).那么只有在水平拖动并达到最小距离时拖动才起作用,否则滚动视图或列表手势会覆盖视图手势

You can set minimumDistance to some value (for instance 30). Then the drag only works when you drag horizontally and reach the minimum distance, otherwise the scrollview or list gesture override the view gesture

.gesture(DragGesture(minimumDistance: 30, coordinateSpace: .local)

这篇关于在 SwiftUI 中向 ScrollView 内的视图添加拖动手势会阻止滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆