修改示例
根据我在下面的评论中收到的 @pawello2222 的深思熟虑的回复,我修改了一个示例来演示我正在解决的问题。
为了演示我遇到的问题,我使用了父母和孩子两个视图。在我的代码中,父视图执行多个步骤,但有时动画子视图在第一步中不可见。然而,当它变得可见时,动画已经呈现出最终状态的外观。您可以在以下示例中看到此行为。
父视图
struct ContentView: View {
@State var firstTime = true
@State var breath = false
var body: some View {
VStack {
// This subview is not displayed until after the first time
if !firstTime {
SecondView(breath: $breath)
}
Spacer()
// A button click simulates the steps in my App by toggling the @Binding var
Button("Breath") {
withAnimation {
self.breath.toggle()
self.firstTime = false
}
}
// This vies shows what happens when the subview is …Run Code Online (Sandbox Code Playgroud) swiftui ×1