0

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 2

1

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 with isActive and use your Bool to make the View active.

import SwiftUI

struct SwitchScreen: View {
    @State var animate = false
    @State var endSplash = false
    var body: some View {
        NavigationView{
            switch endSplash{
            case false:
                ZStack{
                    
                    Color.blue
                    
                    Image(systemName: "checkmark").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)
            case true:
                Text("Done")
            }
        }
    }
    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 ?
            }
        }
    }
}
struct SwitchScreen_Previews: PreviewProvider {
    static var previews: some View {
        SwitchScreen()
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this is what I was searching for
1

I have make some edits on your code, hope it helps.

struct ContentView: View {
    @State var animate = false
    @State var endSplash = false
    var body: some View {
        NavigationView {
            ZStack{
                NavigationLink(
                    destination: YourDestinationView(),
                    isActive: $endSplash
                ) { EmptyView() }
                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 ?
            }
        }
    }
}

1 Comment

Using this way you just need to set what is your destination view, and at the end of the animation it will be navigate to the destination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.