在 SwiftUI 中向 ScrollView 内的视图添加拖动手势会阻止滚动

编程入门 行业动态 更新时间:2024-10-09 22:22:52
本文介绍了在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-30 22:00:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/787973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拖动   视图   手势   中向   SwiftUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!