当我点击另一个视图时,会触发onTapGesture
修改器.
以下是我的观点,归结为一个最小的可重现的例子:
var body: some View {
VStack {
Menu {
ForEach(["Option 1", "Option 2"], id: \.self) { item in
Button(item) {
currentItem = item
expanded.toggle()
}
}
} label: {
HStack {
Text(currentItem ?? "Pick something…")
.animation(.default, value: currentItem)
Spacer()
Image(systemName: "chevron.down")
.rotationEffect(expanded ? .degrees(-180.0) : .zero)
.animation(.default, value: expanded)
}
}
.menuOrder(.fixed)
.onTapGesture { expanded.toggle() }
Divider()
}
}
@State private var currentItem: String?
@State private var expanded = false
(简而言之,有一个带有定制标签的菜单,当菜单展开和折叠时会显示动画.我知道也有DisclosureGroup
个,但它的外观和行为有点不同,不符合我的应用程序的规格.)
When I tap on the Menu
itself, everything works as expected:
However, if I tap below the Menu
, the area of Divider
, the Menu
is not triggered (expected,) but its onTapGesture
unexpectedly is triggered and starts the chevron animation:
I tried to move onTapGesture
inside Menu
's label
, but it's not triggered from there at all:
有谁知道那里发生了什么,以及如何修复它?我现在唯一能想到的就是这是SwiftUI中的一个错误.如有任何指导,我们不胜感激.