Swift UI 自动从初始屏幕导航到另一个屏幕,并带有延迟时间
Swift UI navigation from splash screen to another screen automatically with delay time
我是 IOS 和 Swift UI 的新手,我无法在初始屏幕后导航到登录屏幕。我如何导航到登录屏幕?请帮帮我!!非常感谢您的所有回答。
import SwiftUI
import Dispatch
struct SplashScreen: View {
let login = LoginScreen()
let color = Color.init("black_1")
// let color = Color.white
var body: some View {
let stack = VStack(alignment: .center){
Image("logo")
}.background(color).onAppear(perform: {
gotoLoginScreen(time: 2.5)
})
return stack
}
}
func gotoLoginScreen(time: Double){
DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)) {
print("gotoLoginScreen")
}
return
}
struct SplashScreen_Previews: PreviewProvider {
static var previews: some View {
SplashScreen()
}
}
我想你想要这样的东西:
导入 SwiftUI
struct LoginScreen: View {
var body: some View {
VStack {
Text("Welcome to")
Text("the LoginView")
}.navigationBarTitle("Login")
}
}
struct SplashScreen: View {
@State private var isActive = false
var body: some View {
NavigationView {
VStack {
Image("logo")
NavigationLink(destination: LoginScreen(),
isActive: $isActive,
label: { EmptyView() })
}
.onAppear(perform: {
self.gotoLoginScreen(time: 2.5)
})
}
}
func gotoLoginScreen(time: Double) {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)) {
self.isActive = true
}
}
}
内容视图:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: LoginView()) {
Text("Navigate")
}
}
.navigationBarTitle("LoginView")
.navigationBarHidden(true)
}
}
}
您的登录视图:
struct LoginView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "gobackward").padding()
}
.navigationBarHidden(true)
}
}
考虑到您可能不想在已经进入 LoginScreen
后返回启动画面,我建议采用另一种方法,特别是更改 rootView
UIHostingController
在 SceneDelegate
中。
如果您想推送到下一个屏幕,您可以使用以下代码重构:
struct SplashScreen: View {
@State private var isActive = false
let login = LoginScreen()
let color = Color.init("black_1")
var body: some View {
NavigationView {
VStack(alignment: .center) {
Image("logo")
NavigationLink(destination: login,
isActive: $isActive,
label: { EmptyView() })
}
.background(color)
.onAppear(perform: {
self.gotoLoginScreen(time: 2.5)
})
}
}
func gotoLoginScreen(time: Double) {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)) {
self.isActive = true
}
}
}
这对我有用。
import SwiftUI
struct ContentView: View {
@State var isActive:Bool = false
var body: some View {
VStack {
if self.isActive {
LoginScreen()
} else {
Image("logo")
}
}.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
withAnimation {
self.isActive = true
}
}
}
}
}
struct LoginScreen: View {
var body: some View {
VStack {
Text("Welcome to")
Text("the LoginView")
}
}
}
我是 IOS 和 Swift UI 的新手,我无法在初始屏幕后导航到登录屏幕。我如何导航到登录屏幕?请帮帮我!!非常感谢您的所有回答。
import SwiftUI
import Dispatch
struct SplashScreen: View {
let login = LoginScreen()
let color = Color.init("black_1")
// let color = Color.white
var body: some View {
let stack = VStack(alignment: .center){
Image("logo")
}.background(color).onAppear(perform: {
gotoLoginScreen(time: 2.5)
})
return stack
}
}
func gotoLoginScreen(time: Double){
DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)) {
print("gotoLoginScreen")
}
return
}
struct SplashScreen_Previews: PreviewProvider {
static var previews: some View {
SplashScreen()
}
}
我想你想要这样的东西:
导入 SwiftUI
struct LoginScreen: View {
var body: some View {
VStack {
Text("Welcome to")
Text("the LoginView")
}.navigationBarTitle("Login")
}
}
struct SplashScreen: View {
@State private var isActive = false
var body: some View {
NavigationView {
VStack {
Image("logo")
NavigationLink(destination: LoginScreen(),
isActive: $isActive,
label: { EmptyView() })
}
.onAppear(perform: {
self.gotoLoginScreen(time: 2.5)
})
}
}
func gotoLoginScreen(time: Double) {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)) {
self.isActive = true
}
}
}
内容视图:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: LoginView()) {
Text("Navigate")
}
}
.navigationBarTitle("LoginView")
.navigationBarHidden(true)
}
}
}
您的登录视图:
struct LoginView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "gobackward").padding()
}
.navigationBarHidden(true)
}
}
考虑到您可能不想在已经进入 LoginScreen
后返回启动画面,我建议采用另一种方法,特别是更改 rootView
UIHostingController
在 SceneDelegate
中。
如果您想推送到下一个屏幕,您可以使用以下代码重构:
struct SplashScreen: View {
@State private var isActive = false
let login = LoginScreen()
let color = Color.init("black_1")
var body: some View {
NavigationView {
VStack(alignment: .center) {
Image("logo")
NavigationLink(destination: login,
isActive: $isActive,
label: { EmptyView() })
}
.background(color)
.onAppear(perform: {
self.gotoLoginScreen(time: 2.5)
})
}
}
func gotoLoginScreen(time: Double) {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)) {
self.isActive = true
}
}
}
这对我有用。
import SwiftUI
struct ContentView: View {
@State var isActive:Bool = false
var body: some View {
VStack {
if self.isActive {
LoginScreen()
} else {
Image("logo")
}
}.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
withAnimation {
self.isActive = true
}
}
}
}
}
struct LoginScreen: View {
var body: some View {
VStack {
Text("Welcome to")
Text("the LoginView")
}
}
}