选择器会干扰onTapGesture

时间:2020-05-25 08:56:49

标签: swift swiftui picker

我想澄清一下,我已经找到答案了,但没有发现任何有用的信息。 我已经发布了这个问题,但是由于我为代码提供图像而不是文本,因此我对其进行了修改。

我有以下代码:

| teamstats_id | Trade_id | home_team_corners | away_team_corners | home_team_cards                                                                                                  | away_team_cards                                                   |
+--------------+----------+-------------------+-------------------+------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+
|            1 |        1 |                 7 |                 4 | 74` Hong Jung-Woon, 94` Kim Woo-Seok                                                                             | NONE                                                              |
|            2 |        2 |                 3 |                 1 | 39` Suchy                                                                                                        | 53` Otavio, 73` Arnold, 93` Rousillon                             |
|            3 |        3 |                 5 |                 1 | 24` Kostic, 36` N`Dicka, 59` Silva An., 70` Ilsanker, 89` Kohr                                                   | 9` Elvedi                                                         |
|            4 |        4 |                 2 |                 6 | 66` Filipenko, 85` Nastic                                                                                        | 16` Koanda, 18` Cheboatev, 23` Tetrenko, 85` Koroev               |
|            5 |        5 |                 6 |                 9 | 4` Kuchinski, 9` Bordukov, 31` Bashilov, 47` Nechaev, 82` Yasukevich, 90` Yasukevich, 91` Solanovich, 94` Glebko | 17` Nazarenko, 25` Carioca, 29` Matveenko, 32` Cesar              |
|            6 |        6 |                 5 |                 6 | 22` Tikhonovsky, 67` Sedko, 72` Migunov                                                                          | 87` Vakulich                                                      |
|            7 |        7 |                 0 |                 0 | 40` RED Musayibov                                                                                                | NONE                                                              |
|            8 |        8 |                 2 |                 9 | 54` Moisander, 75` Friedl                                                                                        | 44` Bender                                                        |
|            9 |        9 |                 2 |                 9 | 54` Moisander, 75` Friedl                                                                                        | 44` Bender                                                        |
|           10 |       38 |                 0 |                 0 | NONE                                                                                                             | NONE                                                              |
|           11 |       39 |                 0 |                 0 | 10` Miil, 65` Kelder, 71` Opp, 80` Pajunurm, 86` Schjonnin-Larsen                                                | 51` Khomutov, 52` Subbotin, 59` Volkov, 82` Raudsepp, 93` Demitov |
|           12 |       40 |                 0 |                 0 | 38` Kauber, 87` Sinilaid                                                                                         | 58` Juhkam                                                        |

在此视图中,struct TimeChoiceView: View { @State var buttonText = "None" @State var isPressed = false var text : String = "SomeView" var body: some View{ VStack{ HStack{ VStack(alignment: .leading, spacing: 20){ Text(text).padding(EdgeInsets(top: 0, leading: 15, bottom: 0, trailing: 0)).font(.system(size: 14)) } Spacer() VStack(alignment: .trailing){ Text(buttonText).padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 15)).foregroundColor(.gray).font(.system(size: 14)).onTapGesture { self.isPressed = !self.isPressed self.buttonText = self.buttonText == "None" ? "Undo" : "None" } } } HStack { if isPressed { TimeChoice() } } } } } 是使用以下代码实现的选择器:

TimeChoice

选择器的出现是由struct TimeChoice: View{ @State var hours: Int = 8 @State var minutes: Int = 30 var body: some View { HStack { Spacer() Picker("", selection: $hours){ ForEach(0..<23, id: \.self) { i in Text("\(i)").tag(i) } }.pickerStyle(WheelPickerStyle()).frame(width: 50, height: 70).clipped() Text(":") Picker("", selection: $minutes){ ForEach(0..<60, id: \.self) { i in Text(i < 10 ? "0\(i)" : "\(i)").tag(i) } }.pickerStyle(WheelPickerStyle()).frame(width: 50, height: 70).clipped() Spacer() } } } 中的onTapGesture方法触发的,但是如您所见,尝试再次按下TimeChoiceView按钮不会触发相关的动作。我可以设置选择器,但不能设置按钮。我有理由认为这与干扰选择器的Undo方法有关,因为只有在我按下按钮并且选择器消失后才会发生这种情况。有没有人经历过这种行为?

1 个答案:

答案 0 :(得分:1)

问题在于,提供的代码Picker在打开时与按钮重叠(即使在视觉上也被剪切),因此要处理所有点击事件。

解决方案是将按钮面板放置在上方 (可选)显示选择器视图。经过Xcode 11.4 / iOS 13.4的测试

var body: some View{
    VStack{
        HStack{
            VStack(alignment: .leading, spacing: 20){
                Text(text).padding(EdgeInsets(top: 0, leading: 15, bottom: 0, trailing: 0)).font(.system(size: 14))
            }
            Spacer()
            VStack(alignment: .trailing){
                    Text(buttonText).padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 15)).foregroundColor(.gray).font(.system(size: 14))
                    .onTapGesture {
                        self.isPressed.toggle()
                        self.buttonText = self.buttonText == "None" ? "Undo" : "None"
                    }
            }
        }.zIndex(1)          // << here !!
        HStack {
            if isPressed {
                TimeChoice()
            }
        }
    }
}