按下按钮八秒后导航
navigate after eight seconds from pressing a button
这是我的按钮
Button(action: {
SearchSomeone()
},label: {
NavigationLink(destination: mySearchList()){
HStack(alignment: .center) {
Text("Search")
.font(.system(size: 17))
.fontWeight(.bold)
.foregroundColor(.white)
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.background(
RoundedRectangle(cornerRadius: 25)
.fill(Color("Color"))
.shadow(color: .gray, radius: 2, x: 0, y: 2)
)
}
这个按钮同时执行功能和搜索,因为搜索需要时间,所以我看不到列表,我该如何执行该功能,然后在 8 秒后我在它之后进行导航?谢谢
根据信息,您希望在 8 秒后切换到新视图。此代码应该适合您。
import SwiftUI
struct ContentView: View {
//Variable to see if view should change or not
@State var viewIsShowing = false
var body: some View {
//Detecting if variable is false
if viewIsShowing == false {
//Showing a button that sets the variable to true
Button(action: {
//Makes it wait 8 seconds before making the variable true
DispatchQueue.main.asyncAfter(deadline: .now() + 8.0) {
viewIsShowing = true
}
}) {
//Button text
Text("Search")
.font(.system(size: 17))
.fontWeight(.bold)
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.background(
RoundedRectangle(cornerRadius: 25)
.fill(Color("Color"))
.shadow(color: .gray, radius: 2, x: 0, y: 2)
)
}
} else {
//If the variable equals false, go here
View2()
}
}
}
//Your other view you want to go to
struct View2: View {
var body: some View {
Text("abc")
}
}
这是我的按钮
Button(action: {
SearchSomeone()
},label: {
NavigationLink(destination: mySearchList()){
HStack(alignment: .center) {
Text("Search")
.font(.system(size: 17))
.fontWeight(.bold)
.foregroundColor(.white)
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.background(
RoundedRectangle(cornerRadius: 25)
.fill(Color("Color"))
.shadow(color: .gray, radius: 2, x: 0, y: 2)
)
}
这个按钮同时执行功能和搜索,因为搜索需要时间,所以我看不到列表,我该如何执行该功能,然后在 8 秒后我在它之后进行导航?谢谢
根据信息,您希望在 8 秒后切换到新视图。此代码应该适合您。
import SwiftUI
struct ContentView: View {
//Variable to see if view should change or not
@State var viewIsShowing = false
var body: some View {
//Detecting if variable is false
if viewIsShowing == false {
//Showing a button that sets the variable to true
Button(action: {
//Makes it wait 8 seconds before making the variable true
DispatchQueue.main.asyncAfter(deadline: .now() + 8.0) {
viewIsShowing = true
}
}) {
//Button text
Text("Search")
.font(.system(size: 17))
.fontWeight(.bold)
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.background(
RoundedRectangle(cornerRadius: 25)
.fill(Color("Color"))
.shadow(color: .gray, radius: 2, x: 0, y: 2)
)
}
} else {
//If the variable equals false, go here
View2()
}
}
}
//Your other view you want to go to
struct View2: View {
var body: some View {
Text("abc")
}
}