我想做两件事。首先,如果我点击“TileCell”,我想导航到 SpaceView。
NavigationLink(destination: SpaceView(space: space)) {
TileCell(
image: image,
text: space.name!,
detailText: nil,
isFaded: space.isComplete
)
}
.buttonStyle(PlainButtonStyle())
}
这很好用。但我也想长按 TileCell 来触发不同的 Action 。
NavigationLink(destination: SpaceView(space: space)) {
TileCell(
image: image,
text: space.name!,
detailText: nil,
isFaded: space.isComplete
)
.onLongPressGesture {
action()
}
}
.buttonStyle(PlainButtonStyle())
}
长按手势有效,但我无法再通过点击导航到 SpaceView。任何让两者都工作的帮助将不胜感激。
最佳答案
我建议在 onTapGesture
中手动触发带有 isActive Binding 的 NavigationLink .然后你可以处理点击和长按。
struct ContentView: View {
@State var outputText: String = ""
@State var isActive : Bool = false
var body: some View {
NavigationView {
NavigationLink(destination: SpaceView(), isActive: $isActive) { //<< here use isActive
TileCell()
.onTapGesture {
isActive = true //<< activate navigation link manually
}
.onLongPressGesture {
print("Long press") //<< long press action here
}
}
}
}
}
关于swiftui - 应用 onLongPress 修饰符时,使用 NavigationLink 进行点击导航被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65207417/