回答編集履歴

2

コードの例を載せる

2022/01/19 08:20

投稿

zundaa
zundaa

スコア101

test CHANGED
@@ -3,3 +3,22 @@
3
3
  https://stackoverflow.com/questions/57191013/swiftui-cant-tap-in-spacer-of-hstack
4
4
 
5
5
  `.contentShape(Rectangle())`を追加してあげることでうまく動作するかもしれません。
6
+
7
+ ちゃんと動くかわかりませんが、`ForEach`のコードをそれっぽくしたものを挙げておきます。
8
+
9
+ ```swift
10
+ ForEach(fruits, id: \.self) { fruit in
11
+ HStack {
12
+ if (fruit.isSelected) {
13
+ Image(fruit.imageName)
14
+ .resizable()
15
+ .frame(width: 50, height: 50)
16
+ }
17
+ Text(fruit.name)
18
+ }
19
+ .contentShape(Rectangle())
20
+ .onTapGesture {
21
+ fruit.isSelected.toggle()
22
+ }
23
+ }
24
+ ```

1

文法の修正

2022/01/19 08:13

投稿

zundaa
zundaa

スコア101

test CHANGED
@@ -1,4 +1,5 @@
1
1
  以下と同じ問題かもしれません。
2
- `.contentShape(Rectangle())`を追加してあげればうまく動作するかもしれません。
3
2
 
4
3
  https://stackoverflow.com/questions/57191013/swiftui-cant-tap-in-spacer-of-hstack
4
+
5
+ `.contentShape(Rectangle())`を追加してあげることでうまく動作するかもしれません。