如何在 SwiftUI 视图中初始化绑定 Bool 值

lrr*_*lds 4 swiftui

修改示例

根据我在下面的评论中收到的 @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 being displayed with an intial state of false for the @Binding var
        Spacer()
        SecondView(breath: $breath)
    }
}
Run Code Online (Sandbox Code Playgroud)

}

这是包含动画并使用 @Binding var 来控制动画外观的子视图。

struct SecondView: View {
@Binding var breath: Bool

var body: some View {
    Image(systemName: "flame")
        .resizable()
        .rotationEffect(.degrees(breath ? 360 : 0), anchor: .center)
        .scaleEffect(breath ? 1 : 0.2)
        .opacity(breath ? 1 : 0.75)
        .animation(.easeInOut(duration: 2))
        .foregroundColor(Color.red)
}
Run Code Online (Sandbox Code Playgroud)

}

当您第一次执行此操作时,顶部子视图不会显示,当您单击按钮时,下部子视图将执行预期的动画,然后切换firstTime var,以便顶部子视图变得可见。请注意,动画已完全展开,如果我要执行另一个步骤(单击),并且 @Binding 属性具有相同的 true 值,则视图根本不会改变。这是我正在努力解决的问题。如果第一步是切换 Bool 值的步骤,即使子视图没有显示,我也希望保持子视图不处于最终状态。换句话说,我只想在子视图实际以 true 值显示时才初始化它,以便动画总是从小开始。

这就是为什么我希望子视图将 Binding var 初始化为 false,直到它第一次实际被调用(或将其状态重置为动画的缩小版本),以更可行的方式为准。

paw*_*222 7

看起来您可能想_breath使用提供的参数进行初始化:

struct ContentView: View {
    @Binding var breath: Bool

    init(breath: Binding<Bool>) {
        _breath = breath
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您想使用常量值(在您的示例中false),您可以执行以下操作:

struct ContentView: View {
    @Binding var breath: Bool

    init(breath: Binding<Bool>) {
        _breath = .constant(false)
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,为什么需要breath: Binding<Bool>参数呢?


编辑

以下是如何使用变量控制子视图动画的示例@Binding

struct ContentView: View {
    @State var breath = false

    var body: some View {
        VStack {
            Button("Breath") {
                withAnimation {
                    self.breath.toggle()
                }
            }
            SecondView(breath: $breath)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
struct SecondView: View {
    @Binding var breath: Bool

    var body: some View {
        Image(systemName: "flame")
            .imageScale(.large)
            .rotationEffect(.degrees(breath ? 360 : 0), anchor: .center)
            .scaleEffect(breath ? 1 : 0.2)
            .opacity(breath ? 1 : 0.75)
            .animation(.easeInOut(duration: 2))
    }
}
Run Code Online (Sandbox Code Playgroud)