.actionSheet、.sheet、.fullScreenCover 上的枚举行为不一致,这是一个常见的错误吗?

时间:2021-06-12 19:42:50

标签: swift enums swiftui

有时枚举注册代码有效,有时此代码无效。大多数情况下发生的是前两个(来自 .actionSheet、.sheet、.fullScreenCover),当第三个被调用时,它不会激活工作表。代码如下:

    struct HomeView: View {


        @State var activeSheet: activeSheet?
        @State var activeScreen: activeScreen?
        @State var actionSheet: actionSheet?

                
        var body: some View {
            VStack {
                VStack {
                    Header(activeScreen: $activeScreen)
                    ZStack (alignment: .bottom){
                        Body(activeScreen: $activeScreen)
                        Footer(activeSheet: $activeSheet).environmentObject(viewModel)
                            .frame(width: .infinity, alignment: .bottomLeading)
                            .offset(x: 0, y: footerOffset)
                            .gesture(
                                DragGesture()
                                    .onChanged { gesture in
                                        if gesture.translation.height >= 0 {
                                            footerOffset = 280
                                        } else {
                                            footerOffset = 10
                                        }
                                        print(gesture.translation)
                                    }
                                    .onEnded { _ in
                                    print("ended")
                                }
                            ).animation(.spring())
                    }.adaptsToKeyboard()
                }
                .background(Color(hex: 0xf5f5f5))
                .actionSheet(item: $actionSheet) { item in
                    switch item {
                    case .media:
                        if UIImagePickerController.isSourceTypeAvailable(.camera) {
                            return ActionSheet(title: Text("Select a media source"),
                                               buttons: [
                                                .default(Text("Photo Library")) {
                                                    activeSheet = .activeScreenOne
                                                },
                                                .default(Text("Camera")) {
                                                    activeSheet = .activeScreenTwo
                                                },
                                                .cancel()
                                               ]
                                               )
                        } else {
                            return ActionSheet(title: Text("Select a media source"),
                                               buttons: [
                                                .default(Text("Photo Library")) {
                                                    mapViewActiveSheet = .multipleImage
                                                },
                                                .cancel()
                                               ]
                                               )
                        }
                    }
                }
                .sheet(item: $activeSheet) { item in
                    switch item {
                    case .activeSheetOne:
                        IndividualView(activeSheet: $activeSheet).environmentObject(viewModel)
                    case .activeSheetOne:
                        IndividualView(activeSheet: $activeSheet).environmentObject(viewModel)
                }
                .fullScreenCover(item: $activeScreen) { item in
                    switch item {
                    case .activeScreenOne:
                        View(activeScreen: $activeScreen)
                    case .activeScreenTwo:
                        View(activeScreen: $activeScreen)
                    }
                }
            }
            .environmentObject(viewModel)
            .ignoresSafeArea()
        }
    }

问题

有没有更好的方法来做到这一点?

这是常见的行为吗?我只是在 swiftUI 上承担了太多责任?

有人知道为什么这行不通吗?

如果需要更多代码来帮助澄清错误,请告诉我,将上传。真的很想搞清楚这件事。

0 个答案:

没有答案