动画进入背景视图而不是前景

时间:2020-05-31 15:14:34

标签: ios swift swiftui

我有以下内容:

struct SettingsView: View {
    @State private var showSuccessSave: Bool = false

    var body: some View {
        NavigationView {
            ScrollView {
                VStack {
                    SuccessNotificationView()
                    .offset(y: self.showSuccessSave ? -UIScreen.main.bounds.height/9 : -UIScreen.main.bounds.height)
                        .animation(
                            .interactiveSpring(
                                response: 0.8,
                                dampingFraction: 0.8,
                                blendDuration: 1
                            )
                    )
                    .onTapGesture {
                        self.showSuccessSave = false
                    }


                    Button(action: {
                        self.showSuccessSave.toggle()

                    }) {
                        Text("Save")
                    }
                }
            }
            .navigationBarTitle("Settings")
        }
    }
}

我调用的函数:

struct SuccessNotificationView: View {
    var body: some View {
        Text("Success")
            .padding()
            .foregroundColor(Color.white)
            .frame(width: UIScreen.main.bounds.width, height: 100)
            .background(Color.green)
            .cornerRadius(20)
    }
}

弹出时,请查看设置文本:

enter image description here

如何使该通知位于设置文本的最前面?现在,它会在该视图的顶部显示所有内容。

1 个答案:

答案 0 :(得分:1)

这是可行的方法。使用Xcode 11.4 / iOS 13.4进行了测试

demo

struct SettingsView: View {
    @State private var showSuccessSave: Bool = false

    var body: some View {
        ZStack(alignment: .top) {
            NavigationView {
                ScrollView {
                    VStack {
                        Button(action: {
                            self.showSuccessSave.toggle()

                        }) {
                            Text("Save")
                        }
                    }
                }
                .navigationBarTitle("Settings")
            }

            SuccessNotificationView()
            .offset(y: self.showSuccessSave ? 0 : -UIScreen.main.bounds.height)
                .animation(
                    .interactiveSpring(
                        response: 0.8,
                        dampingFraction: 0.8,
                        blendDuration: 1
                    )
            )
            .onTapGesture {
                self.showSuccessSave = false
            }
        }
    }
}