swiftui 转换不适用于提取的视图

swiftui transition not work on extracted view

我只是构建了一个带有 .spring() 动画的小弹出窗口,我想稍后在我的应用程序中使用它,但向后过渡并不平滑。它只是从层次结构中消失了。所以这是我的代码:

struct TestPopUp: View {


@State var screen: Bool = false

var body: some View {
        ZStack {
            Color.white
                .edgesIgnoringSafeArea(.all)
            
            VStack {
                Button("Click") {
                    withAnimation(.spring()) {
                        screen.toggle()
                    }
                   
                }
                .font(.largeTitle)

                if screen {
                    NewScreen(screen: $screen)
                        .padding(.top, 300)
                        .transition(.move(edge: .bottom))
                }
}

struct NewScreen: View {
    
    @Binding var screen: Bool
    
    var body: some View {
    ZStack(alignment: .topLeading) {
            Color.black
                .edgesIgnoringSafeArea(.all)
            
            Button {
                screen.toggle()
            } label: {
                Image(systemName: "xmark")
                    .foregroundColor(.white)
                    .font(.largeTitle)
                    .padding(20)
            }

            
        }
    }
}

Transition popup

正如您在视频中看到的那样,视图消失了。但我想要向后进行相同的转换。

您必须为过渡设置动画,进出。因此,NewScreen 变为:

struct NewScreen: View {
    
    @Binding var screen: Bool
    
    var body: some View {
        ZStack(alignment: .topLeading) {
            Color.black
                .edgesIgnoringSafeArea(.all)
            
            Button {
                withAnimation(.spring()) { // Animate here!
                    screen.toggle()
                }
            } label: {
                Image(systemName: "xmark")
                    .foregroundColor(.white)
                    .font(.largeTitle)
                    .padding(20)
            }
        }
    }
}