SwiftUI_visionOS_Note

.sheet() vs .transition() vs .animation

Jan 2, 2024
1 min read|
  • Three different ways to pop up the screen.
    • End result will be same
  • sheet, transition and animation
  • we have to know where it will be better for the app.

Previous .transition() note, we only used a single button to toggle the screen.

This time we will use @Binding and other button on the new screen to toggle.

Write a base code of transition example.

//
//  ContentView.swift
//  Popover
//
//  Created by Yoon Ro on 2024-01-01.
//

import SwiftUI

struct ContentView: View {

    @State var showNewScreen: Bool = false

    var body: some View {
        ZStack{
            Color.clear
            
            VStack{
                Button("Button"){
                    withAnimation(Animation.default){
                        showNewScreen.toggle()
                    }
                }.font(.largeTitle)
                    .padding(.top, 20)
            Spacer()
            }

            
//            METHOD 2 - TRANSITION
    
                if showNewScreen {
                    NewScreen(showNewScreen: $showNewScreen)
                        .padding(.top,100)
                    .transition(AnyTransition.slide.animation(.easeInOut(duration: 1)))            }
//            
//              METHOD 3 - ANIMATION OFFST
//            NewScreen(showNewScreen: $showNewScreen)
//                .offset(y: showNewScreen ? 100 : 700)
//                .frame(depth: 500)
       
        }.background(Color.gray)
            .clipShape(RoundedRectangle(cornerRadius: 25.0))
    }
}

struct NewScreen: View {
    

    @Binding var showNewScreen: Bool
    
    var body: some View {
        ZStack{
            Color.purple
            

            Button(action: {showNewScreen.toggle()}, label: {
                Image(systemName: "xmark")
            })
        }
    }
}
#Preview {
    ContentView()
}
swift

For your information, it appears really impressive.

Subscribe to our newsletter

Get the latest news and updates from our team