SwiftUI过渡期间的子视图偏移动画?

时间:2020-11-10 10:46:01

标签: swiftui

如何在过渡期间为子视图的相对偏移设置动画?在过渡期间为状态设置动画时,它似乎会覆盖过渡设置的位置。

struct ContentView: View {
    @State var showPageTwo = false
    var body: some View {
        ZStack {
            Button("Show page two") { showPageTwo = true }
            if showPageTwo {
                PageTwo() { showPageTwo = false }
                .zIndex(1.0)
                .animation(.linear(duration: 1))
                .transition(.move(edge: .bottom))
            }
        }
    }
}

struct PageTwo: View {
    @State var buttonOffset: CGFloat = 200
    let dismissAction: () -> Void
    var body: some View {
        ZStack {
            Color.gray.edgesIgnoringSafeArea(.all)
            Button("Dismiss", action: dismissAction)
                .offset(x: buttonOffset, y: buttonOffset)
                .animation(.easeIn)
                .onAppear {
                    buttonOffset = 0
                }
        }
    }
}

Offset during transition

0 个答案:

没有答案