SwiftUI中常用的三种弹出方式

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)
            }
        })
    }
}

效果:

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值