fun*_*way 8 macos touch gesture swiftui
我想制作一个列表视图,可以拖动其中的内容进行重新排序并点击以获取详细信息。但是,当我在内容视图中添加 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)
}
}
Run Code Online (Sandbox Code Playgroud)
是否有任何解决方案可以使点击和移动动作协同工作?
作为解决方法,您可以使行的某些特定部分可单击,例如下面示例中的文本(或一些添加的自定义形状或普通按钮)
VStack {
Text("user: \(user)").font(.headline)
.onTapGesture(perform: {
print("tap!")
})
Divider()
}.background(Color.gray)
Run Code Online (Sandbox Code Playgroud)
替代:您可以添加辅助自定义视图作为行覆盖,它将处理视图的点击/鼠标按下操作(并且不会破坏药物)
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
并使用它
VStack {
Text("user: \(user)").font(.headline)
Divider()
}.background(Color.gray)
.overlay(TapHandler()) // << here !!
Run Code Online (Sandbox Code Playgroud)
使用 Xcode 12.0 / macOS 10.15.6 进行测试