小编fun*_*way的帖子

如果内容视图有点击手势操作,SwiftUI ForEach 的 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有任何解决方案可以使点击和移动动作协同工作?

macos touch gesture swiftui

8
推荐指数
1
解决办法
1674
查看次数

标签 统计

gesture ×1

macos ×1

swiftui ×1

touch ×1