onTapGesture劫持事件是否发生?

时间:2019-08-06 16:56:34

标签: swiftui

我正在尝试使用onTapGesture,但这样做几乎使访问列表变得不可能-如果我删除了onTapGesture,它将可以正常工作

这就是我正在使用的东西:

struct NavPickerView: View {
    @State var selected: Int = -1
    @State var cities: [String] = ["New York", "San Diego", "Houston", "Boston"]
    @State var selectionStarted: Bool = false
    var body: some View {
        VStack {
            Text("\(selectionStarted ? "SELECTING" : "WAITING")")
            NavigationView {
                Form {
                    Section {
                        Picker(selection: $selected, label: Text("Search for...")) {
                            Text("Select City").tag(-1)
                            ForEach(0..<cities.count) {
                                Text(verbatim: self.cities[$0]).tag($0)
                            }
                        }
                    }
                }
                .onTapGesture { self.selectionStarted = true }
                .navigationBarTitle(Text("Cities"))
            }
        }
    }
}

在选择一项而不使列表几乎不可访问时运行代码的任何方式吗?

1 个答案:

答案 0 :(得分:0)

对于您来说,.onAppear似乎更合适:

Form {
    Section {
        Picker(selection: $selected, label: Text("Search for...")) {
            Text("Select City").tag(-1)
            ForEach(0..<cities.count) {
                Text(verbatim: self.cities[$0]).tag($0)
            }
            .onAppear { self.selectionStarted = true }
        }
    }
}