skip to Main Content

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


  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()
        }
    }
    
    Login or Signup to reply.
  2. 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 ?
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search