The need is to go to View1
by default and when onLongPressGesture
to View2
. @State
is used to make it. And the code is as following:
@State var showingView2 = false
if self.showingView2 {
NavigationLink(destination: View2())
{ Cell(name: View2.name)
.onLongPressGesture(minimumDuration: 1) {
self.showingView2 = true
}
}
} else {
NavigationLink(destination: View1())
{ Cell(name: View1.name)
.onLongPressGesture(minimumDuration: 1) {
self.showingView2 = true
}
}
}
For the simplicity, new button cannot be added in the UI. I know the code cannot run, but I do not know how to fix it.
Any suggestion will help me. Thanks!
=========================================
Updated
Thanks for @Asperi!
But in my project there is ForEach
in NavigationView
, in which case the answer does not work. The code is following:
import SwiftUI
struct ContentView: View {
@State private var isLongPressed = false
let lyrics = ["a", "b", "c"]
var body: some View {
NavigationView {
List() {
ForEach(0..<lyrics.count) { (index) in
NavigationLink(destination:
Group {
if self.isLongPressed { Destination2() }
else { Destination1() }
})
{ Text(self.lyrics[index]) }
.simultaneousGesture(LongPressGesture(minimumDuration: 1).onEnded { flag in
self.isLongPressed.toggle()
})
}
}
}
}
}
struct Destination1: View {
var body: some View {
Text("Destination1")
}
}
struct Destination2: View {
var body: some View {
Text("Destination2")
}
}
Then how to make it in this case? And I do not want to change the short tap
or long tap
's function, just short tap
to View1
and long tap
to View2
. Thanks!