如果内容视图有轻击手势动作,则SwiftUI for Each的onMove()修饰符停止工作 [英] SwiftUI ForEach's onMove() modifier stop working if the content view has tap gesture action
本文介绍了如果内容视图有轻击手势动作,则SwiftUI for Each的onMove()修饰符停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个列表视图,可以拖动哪些内容进行重新排序并点击查看详细信息。 但是当我在内容视图中添加onTapGesture()操作时,onMove()操作停止工作。
示例代码:
import SwiftUI
struct TestTapAndMove: View {
@State private var users = ["Paul", "Taylor", "Adele"]
var body: some View {
List {
ForEach(users, id: .self) { user in
VStack {
Text("user: (user)").font(.headline)
Divider()
}.background(Color.gray)
// The TapGesture will result in onMove action not working.
.onTapGesture(perform: {
print("tap!")
})
}
.onMove(perform: move)
}
}
func move(from source: IndexSet, to destination: Int) {
print("onMove!")
users.move(fromOffsets: source, toOffset: destination)
}
}
是否有任何解决方案可以使点击和移动操作协同工作?
推荐答案
作为解决办法,您可以使行的某些特定部分可单击,如下例中的文本(或某些添加的自定义形状或纯按钮)
VStack {
Text("user: (user)").font(.headline)
.onTapGesture(perform: {
print("tap!")
})
Divider()
}.background(Color.gray)
备用:您可以将帮助器自定义视图添加为行覆盖,它将处理视图的点击/鼠标按下操作(并且不会破坏药物)
class TapHandlerView: NSView {
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event) // keep this to allow drug !!
print("tap")
}
}
struct TapHandler: NSViewRepresentable {
func makeNSView(context: Context) -> TapHandlerView {
TapHandlerView()
}
func updateNSView(_ nsView: TapHandlerView, context: Context) {
}
}
并使用它
VStack {
Text("user: (user)").font(.headline)
Divider()
}.background(Color.gray)
.overlay(TapHandler()) // << here !!
使用Xcode 12.0/MacOS 10.15.6测试
这篇关于如果内容视图有轻击手势动作,则SwiftUI for Each的onMove()修饰符停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文