
Sheet & FullScreenCover & Transition & Animation
이번에는 Sheet & FullScreenCover & Transition & Animation를 비교해보도록 하겠습니다.
위 수정자를 사용해서 모두 동일하게 sheet
의 효과를 줄 수 있습니다. 간단하게 sheet
부터 시작하도록 하겠습니다.
Sheet
sheet
수정자는 sheetView
에 presentationMode
를 적용시켜 sheet
뷰에서 "X" 버튼을 누르면 닫힐 수 있도록 만들어 줬습니다.
import SwiftUI // MARK : Body struct ContentView: View { @State var showView: Bool = false var body: some View { ZStack(alignment: .top) { Color.yellow .ignoresSafeArea() VStack { Button(action: { showView.toggle() }) { Text("Some Sheet") .font(.title) .foregroundColor(.black) } } // METHOD 1 - SHEET .sheet(isPresented: $showView, content: { sheetView() }) } } } // SheetView struct sheetView: View { @Environment(\.presentationMode) var presentationMode var body: some View { ZStack(alignment: .topLeading) { Color("Paleblue") .ignoresSafeArea() Button(action: { presentationMode.wrappedValue.dismiss() }) { Image(systemName: "xmark") .font(.title) .foregroundColor(.white) .padding(20) } } } } // MARK : PrieView struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }

FullScreenCover
fullscreenCover
은 Sheet
와 코드가 동일하지만 METHOD부분 코드를 fullScreenCover
로 수정해주겠습니다.
// METHOD 2 - FullScreenCover .fullScreenCover(isPresented: $showView, content: { sheetView() })

Transition
transition
을 사용해서 sheet
효과와 비슷하게 구현할 수 있지만 조금 복잡할 수 있습니다. 저번 게시물에서 다뤘듯이 transition
을 사용하려면 조건이 반드시 있어야 합니다.
우선 if
문으로 showView
가 true
이면 sheetView
를 실행할 수 있도록 해 주고 top
패딩을 100으로 설정해 sheet
처럼 보이도록 설정해줍니다. 그리고 sheetView
가 bottom
에서 등장할 수 있게 해 줬습니다.
또 sheet
에서 사용했던 presentationMode
를 삭제하고 @Binding
으로 showView
를 불러와 Button
에 showView.toggle()
을 추가해줬습니다.
import SwiftUI // MARK : Body struct ContentView: View { @State var showView: Bool = false var body: some View { ZStack(alignment: .top) { Color.yellow .ignoresSafeArea() VStack { Button(action: { showView.toggle() }) { Text("Some Sheet") .font(.title) .foregroundColor(.black) } } // METHOD 3 - TRANSITION if showView { sheetView(showView: $showView) .padding(.top, 100) .transition(AnyTransition.move(edge: .bottom)) .animation(.spring()) } } } } // SheetView struct sheetView: View { @Binding var showView: Bool var body: some View { ZStack(alignment: .topLeading) { Color("Paleblue") .ignoresSafeArea() Button(action: { showView.toggle() }) { Image(systemName: "xmark") .font(.title) .foregroundColor(.white) .padding(20) } } } } // MARK : PrieView struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
아래 결과 화면을 보면 sheetView
가 나타날 때는 이상 없이 애니메이션 효과가 적용된 것을 볼 수 있지만 "X" 를 눌러서 닫을 때에는 애니메이션 효과가 제대로 적용되지 않았습니다.

이것은 ZStack
과 .zIndex
를 사용하면 해결할 수 있습니다. if
문을 ZStack
으로 덮어주고 ZStack
외부에 .zIndex(2.0)
을 추가해줍니다. 그러면 VStack
보다 ZStack
이 최상위로 오게 되는 것이죠.
// METHOD 3 - TRANSITION ZStack { if showView { sheetView(showView: $showView) .padding(.top, 100) .transition(AnyTransition.move(edge: .bottom)) .animation(.spring()) } } .zIndex(2.0)

Animation
마찬가지로 Animation
은 삼항연산자를 사용해서 구현할 수 있습니다.
// METHOD 4 - Animation sheetView(showView: $showView) .padding(.top, 100) .offset(y: showView ? 0 : UIScreen.main.bounds.height) .animation(.spring())

읽어주셔서 감사합니다🤟
SwiftUI : 애니메이션과 비슷한 Transitions
SwiftUI : Sheet / FullScreenCover (@Environment / presentationMode)
'SWIFTUI > Others' 카테고리의 다른 글
SwiftUI : Markups과 Documentation을 추가하는 방법 (0) | 2021.05.13 |
---|---|
SwiftUI : ActionSheet (0) | 2021.05.12 |
SwiftUI : 애니메이션과 비슷한 Transitions (0) | 2021.05.10 |
SwiftUI : Animation #3 (0) | 2021.05.10 |
SwiftUI : Identifiable을 사용하여 연락처 뷰 (0) | 2021.05.03 |