Hey I am new to Swift and SwiftUI and I would like to know how I can switch between Views
As you can see in the code below I have made a splashscreen animation and I would like my App to switch into the next View when the state "endSplash" is true, but I do not know how to achieve this goal. I have read on how to change the View with a NavigationView and a NavigationLink but the problem is I do not want the user to press for example on a button/text I want to switch the View directly. Is there like a function I can call to change the View directly (like in Android Studio where I can just start a new 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 ?
}
}
}
}
2
Answers
Since you already have a
Bool
all you need is a conditional.If you want it in the Navigation Stack (with a Back button) use the
NavigationLink
constructor withisActive
and use yourBool
to make theView
active.I have make some edits on your code, hope it helps.