gpt4 book ai didi

swiftui - 点击 NavigationLink 后如何执行操作?

转载 作者:行者123 更新时间:2023-12-02 11:01:52 34 4
gpt4 key购买 nike

我的第一个 View 中有一个加号按钮。看起来像一个 FAB 按钮。我想在点击 NavigationLink 中包含的某个步骤后隐藏它。到目前为止我有这样的事情:

ForEach(0 ..< 12) {item in
NavigationLink(destination: TransactionsDetailsView()) {
VStack {
HStack(alignment: .top) {
Text("List item")
}
.padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
.foregroundColor(.black)
Divider()
}
}
.simultaneousGesture(TapGesture().onEnded{
self.showPlusButton = false
})
.onAppear(){
self.showPlusButton = true
}
}

单击一下即可正常工作。但当我长按 NavigationLink 时,它不起作用。我应该如何重写我的代码以包括长按?或者也许我应该让它的工作方式与使用simultaneousGesture不同?

最佳答案

是的,NavigationLink 不允许此类同步手势(可能是设计好的,可能是由于问题,等等)。

您期望的行为可能会实现如下(当然,如果您需要在列表项中添加一些 V 形符号,则需要手动添加)

struct TestSimultaneousGesture: View {
@State var showPlusButton = false
@State var currentTag: Int?
var body: some View {

NavigationView {
List {
ForEach(0 ..< 12) { item in
VStack {
HStack(alignment: .top) {
Text("List item")
NavigationLink(destination: Text("Details"), tag: item, selection: self.$currentTag) {
EmptyView()
}
}
.padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
.foregroundColor(.black)
Divider()
}
.simultaneousGesture(TapGesture().onEnded{
print("Got Tap")
self.currentTag = item
self.showPlusButton = false
})
.simultaneousGesture(LongPressGesture().onEnded{_ in
print("Got Long Press")
self.currentTag = item
self.showPlusButton = false
})
.onAppear(){
self.showPlusButton = true
}
}
}
}
}
}

关于swiftui - 点击 NavigationLink 后如何执行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58897453/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com