[英]How to change background color of the NavigationView in SwiftUI?
[英]How to change NavigationView's destination in SwiftUI?
默認情況下需要 go 到View1
,當onLongPressGesture
到View2
時。 @State
用於制作它。 代碼如下:
@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
}
}
}
為簡單起見,無法在 UI 中添加新按鈕。 我知道代碼無法運行,但我不知道如何修復它。
任何建議都會幫助我。 謝謝!
==========================================
更新
感謝@Asperi!
但是在我的項目中NavigationView
中有ForEach
,在這種情況下答案不起作用。 代碼如下:
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")
}
}
那么在這種情況下如何制作呢? 而且我不想更改short tap
按或long tap
按的 function,只需short tap
按到View1
和long tap
按到View2
。 謝謝!
這是一個可能的方法的演示。 使用 Xcode 11.4 / iOS 13.4 測試
struct TestAlternateDestinations: View {
@State private var isLongPressed = false
var body: some View {
NavigationView {
NavigationLink(destination: Group {
if isLongPressed { Destination2() }
else { Destination1() }}) {
Text(self.isLongPressed ? "Link2" : "Link1")
}
.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")
}
}
這是基於@Asperi 的最終答案。
而且還有一點小bug。 如果沒有在NavigationLink
的名稱中添加很多空格,則長按僅在您點擊“a”“b”“c”時起作用。 在“a”“b”“c”之外的 NavigationLink 的其他空間上,所有的點擊都是短點擊。
import SwiftUI
struct ContentView: View {
@State private var isLongPressed = false
@State var currentTag: Int?
let lyrics = ["a", "b", "c"]
var body: some View {
NavigationView {
List {
ForEach(0..<lyrics.count) { index in
VStack{
HStack(alignment: .top) {
NavigationLink(destination: Group
{ if self.isLongPressed { Destination2() } else { Destination1() } }, tag: index, selection: self.$currentTag
) {
// This is still a little bug. If not add many space, the long tap only work when you tap on the "a" "b" "c". On other space of the NavigationLink out of the "a" "b" "c", all tap is short tap.
Text(self.lyrics[index] + " ")
}
}
}.simultaneousGesture(LongPressGesture().onEnded { _ in
print("Got Long Press")
self.currentTag = index
self.isLongPressed = true
})
.simultaneousGesture(TapGesture().onEnded{
print("Got Tap")
self.currentTag = index
self.isLongPressed = false
})
.onAppear(){
self.isLongPressed = false
}
}
}
}
}
}
struct Destination1: View {
var body: some View {
Text("Destination1")
}
}
struct Destination2: View {
var body: some View {
Text("Destination2")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.