动画不适用于SwiftUI视图状态更改

时间:2020-02-25 22:04:30

标签: swift animation swiftui xcode11

我正在尝试动画化对应用于SwiftUI TextField的叠加层的更改。叠加层的状态由状态变量(简单的布尔值)控制,大多数参考建议在Toggle控件中向此变量的绑定添加.animation()即可。但是,它似乎没有作用。我尝试将相同的修饰符添加到用于叠加层的视图中,并将叠加层的组件视图包装在动画块中-再次,不高兴。

有趣的是,我已经注意到在画布或模拟器中运行代码时出现的错误,这可能很重要:

2020-02-25 22:00:19.360009+0000 Sandbox[9392:671490] invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.

代码如下(它的原型设计有点粗糙且准备就绪)。谁能告诉我我所缺少的吗?

import SwiftUI

struct ContentView: View {
    @State private var isValid = true

    var body: some View {
        VStack {
            Form {
                TextField("Placeholder", text: .constant(""))
                    .padding(6)
                    .background(ErrorView(isValid: $isValid)
                    .overlay(RoundedRectangle(cornerRadius: 4)
                            .stroke(Color(.systemGray3), lineWidth: 1))
                    .padding()

                Toggle(isOn: $isValid.animation(.easeInOut), label: { Text("Toggle Valid") })
            }
        }
    }
}

struct ErrorView: View {
    @Binding var isValid: Bool

    var body: some View {
        if isValid {
            return AnyView(EmptyView())
        } else {
            return AnyView(
                ZStack {
                    HStack {
                        Spacer()
                        Image(systemName: "exclamationmark.triangle.fill")
                            .foregroundColor(Color.red)
                            .padding(.trailing, 8)
                    }

                    HStack {
                        Spacer()
                        Text("error message")
                            .font(.caption)
                            .foregroundColor(Color.red)
                            .offset(x: 0, y: -28)
                    }
                }
            )
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是一种可行的方法,并且经过了简化((已测试并与Xcode 11.2 / iOS 13.2一起使用)

demo

struct ContentView: View {
    @State private var isValid = true

    var body: some View {
        VStack {
            Form {
                TextField("Placeholder", text: .constant(""))
                    .padding(6)
                    .background(ErrorView().opacity(isValid ? 0.0 : 1.0))
                    .overlay(RoundedRectangle(cornerRadius: 4)
                            .stroke(Color(.systemGray3), lineWidth: 1))
                    .padding()

                Toggle(isOn: $isValid.animation(), label: { Text("Toggle Valid") })
            }
        }
    }
}

struct ErrorView: View {

    var body: some View {
        ZStack {
            HStack {
                Spacer()
                Image(systemName: "exclamationmark.triangle.fill")
                    .foregroundColor(Color.red)
                    .padding(.trailing, 8)
            }

            HStack {
                Spacer()
                Text("error message")
                    .font(.caption)
                    .foregroundColor(Color.red)
                    .offset(x: 0, y: -28)
            }
        }
    }
}