在SwiftUI中向ScrollView内部的View添加拖动手势会阻止滚动 [英] Adding a drag gesture in SwiftUI to a View inside a ScrollView blocks the scrolling
问题描述
所以我有一个 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中,就像在 shouldRecognizeSimultaneouslyWith
方法中指定 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相同(拖动)
.我还尝试为提供所有可能的静态
参数–我可以滚动工作,也可以拖动手势工作.永远都不要. 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内部的View添加拖动手势会阻止滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!