【发布时间】:2021-03-24 13:30:53
【问题描述】:
嘿,我是 Swift 和 SwiftUI 的新手,我想知道如何在视图之间切换
正如您在下面的代码中看到的那样,我制作了一个闪屏动画,我希望我的应用程序在状态“endSplash”为真时切换到下一个视图,但我不知道如何实现这个目标。我已经阅读了如何使用 NavigationView 和 NavigationLink 更改视图,但问题是我不希望用户按下例如我想直接切换视图的按钮/文本。有没有像我可以调用的函数来直接更改视图(比如在 Android Studio 中我可以开始一个新的 Intent)?
struct ContentView: View {
@State var animate = false
@State var endSplash = false
var body: some View {
NavigationView{
ZStack{
Color("Primary")
Image("LBig").resizable().renderingMode(.original).aspectRatio(contentMode: animate ? .fill : .fit)
.frame(width: animate ? nil : 85, height: animate ? nil: 85)
.colorInvert().scaleEffect(animate ? 3 : 1).frame(width: UIScreen.main.bounds.width)
}.ignoresSafeArea(.all, edges: .all).onAppear(perform: {
animateSplash()
}).opacity(endSplash ? 0:1)
}
}
func animateSplash(){
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
withAnimation(Animation.easeOut(duration: 0.45)){
animate.toggle()
}
withAnimation(Animation.linear(duration: 0.35)){
endSplash.toggle()
//Switch to another View here I guess ?
}
}
}
}
【问题讨论】: