【问题标题】:Color Change Animation颜色变化动画
【发布时间】:2020-03-05 14:49:30
【问题描述】:

我正在尝试为某些文本设置颜色变化的动画,但我似乎无法让它逐渐变化。我已经尝试过隐式和显式动画,如下面的代码所示,但没有骰子......

    struct Example: View {

    @State var showing = false

    var body: some View {
        VStack {
            Text("test text").foregroundColor(showing ? .red : .blue)
                .animation(.easeIn(duration: 2))
            Button(action: toggle) {
                Text("Toggle")
            }
        }
    }

    func toggle() {
        withAnimation(.easeIn(duration: 2)) {self.showing.toggle()}
    }

}

谁能指点一下?

【问题讨论】:

标签: swiftui


【解决方案1】:

很遗憾,您无法为.foregroundColor 制作动画。但是您可以为.colorMultiply 制作动画。因此,在您的情况下,这将起作用:

struct ColorChangeAnimation: View {

    @State private var multiplyColor: Color = .blue
    var body: some View {
        VStack {
            Text("test text")
                .foregroundColor(.white)
                .colorMultiply(multiplyColor)

            Button(action: toggle) {
                Text("Toggle")
            }
        }
    }

    func toggle() {
        withAnimation(.easeIn(duration: 2)) {
            self.multiplyColor = (self.multiplyColor == .red) ? .blue : .red
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-15
    • 2014-01-19
    • 1970-01-01
    • 2013-08-15
    • 2011-04-26
    • 2011-05-05
    • 2015-08-11
    • 2012-04-02
    相关资源
    最近更新 更多