SwiftUI中三种常用的弹出方式
老规矩上代码:
import SwiftUI
struct PopoverBootCamp: View {
@State var showNewScreen = false
var body: some View {
ZStack{
Color.orange.ignoresSafeArea()
VStack {
Button(action: {
withAnimation(Animation.spring) {
showNewScreen.toggle()
}
}, label: {
Text("Button".uppercased())
.font(.largeTitle)
//.foregroundColor(.white)
})
Spacer()
}
/// 1- sheet
// .sheet(isPresented: $showNewScreen, content: {
// NewPage()
// })
/// 2-transition
// ZStack(content: {
// if showNewScreen {
// NewPage(showNewScreen: $showNewScreen)
// .padding(.top, 100)
// .transition(.move(edge: .bottom))
//
// }
// }).zIndex(2.0)
/// 3- aniamtion offset
NewPage(showNewScreen: $showNewScreen)
.padding(.top, 100)
.animation(.spring) { view in
view.offset(y: showNewScreen ? 0 : UIScreen.main.bounds.height)
}
}
}
}
#Preview {
PopoverBootCamp()
}
struct NewPage: View {
@Binding var showNewScreen: Bool
@Environment(\.presentationMode) var presentationMode
var body: some View{
ZStack(alignment: .topLeading, content: {
Color.pink
.ignoresSafeArea()
Button {
/// 1- sheet
// presentationMode.wrappedValue.dismiss()
/// 2-transition
withAnimation(Animation.spring) {
showNewScreen.toggle()
}
} label: {
Image(systemName: "xmark")
.font(.title)
.foregroundColor(.white)
.padding(20)
}
})
}
}
效果: