SwiftUI:动画文本颜色-前景色()

时间:2019-09-07 09:35:06

标签: ios swift swiftui

我一直在尝试对UI的各个部分进行动画处理,但是似乎您无法为SwiftUI Text的前景颜色设置动画?我想在状态改变时平稳地切换某些文本的颜色。如果我为“文本”周围的视图设置背景颜色动画,则此方法很好,但是前景色不起作用。有没有人有幸为这样的变化做动画?不确定这是Xcode beta错误还是预期的功能...

Text(highlightedText)
    .foregroundColor(color.wrappedValue)
    .animation(.easeInOut)

// Error: Cannot convert value of type 'Text' to closure result type '_'

4 个答案:

答案 0 :(得分:5)

有一个更简单的方法,它借鉴了Apple的“动画视图和过渡”教程代码。 HikeGraph中的GraphCapsule实例化证明了这一点。

foregroundColor不能动画,colorMultiply可以动画。将前景色设置为白色,然后使用colorMultiply设置所需的实际颜色。要从红色动画为蓝色:

struct AnimateDemo: View {
    @State private var color = Color.red

    var body: some View {
        Text("Animate Me!")
            .foregroundColor(Color.white)
            .colorMultiply(self.color)
            .onTapGesture {
                withAnimation(.easeInOut(duration: 1)) {
                    self.color = Color.blue
                }
        }
    }
}

struct AnimateDemo_Previews: PreviewProvider {
    static var previews: some View {
        AnimateDemo()
    }
}

答案 1 :(得分:1)

enter image description here

Text的颜色属性无法在SwiftUIUIKit中设置动画。 但您可以达到所需的结果:

struct ContentView: View {

    @State var highlighted = false

    var body: some View {
        VStack {
            ZStack {
            // Highlighted State
            Text("Text To Change Color")
                .foregroundColor(.red)
                .opacity(highlighted ? 1 : 0)

            // Normal State
            Text("Text To Change Color")
                .foregroundColor(.blue)
                .opacity(highlighted ? 0 : 1)
            }
            Button("Change") {
                withAnimation(.easeIn) {
                    self.highlighted.toggle()
                }
            }
        }
    }
}

您可以将此功能封装在自定义View中,并在您喜欢的任何地方使用。

答案 2 :(得分:1)

SwiftUI中有一个不错的协议,可让您设置任何动画。甚至是不可动画的东西! (例如文字颜色)。该协议称为AnimatableModifier。

如果您想了解更多信息,我写了整篇文章解释其工作原理:https://swiftui-lab.com/swiftui-animations-part3/

下面是一个有关如何实现这种视图的示例:

AnimatableColorText(from: UIColor.systemRed, to: UIColor.systemGreen, pct: flag ? 1 : 0) {
    Text("Hello World").font(.largeTitle)
}.onTapGesture {
    withAnimation(.easeInOut(duration: 2.0)) {
      self.flag.toggle()
    }
}

执行:

struct AnimatableColorText: View {
    let from: UIColor
    let to: UIColor
    let pct: CGFloat
    let text: () -> Text

    var body: some View {
        let textView = text()

        // This should be enough, but there is a bug, so we implement a workaround
        // AnimatableColorTextModifier(from: from, to: to, pct: pct, text: textView)

        // This is the workaround
        return textView.foregroundColor(Color.clear)
            .overlay(Color.clear.modifier(AnimatableColorTextModifier(from: from, to: to, pct: pct, text: textView)))
    }

    struct AnimatableColorTextModifier: AnimatableModifier {
        let from: UIColor
        let to: UIColor
        var pct: CGFloat
        let text: Text

        var animatableData: CGFloat {
            get { pct }
            set { pct = newValue }
        }

        func body(content: Content) -> some View {
            return text.foregroundColor(colorMixer(c1: from, c2: to, pct: pct))
        }

        // This is a very basic implementation of a color interpolation
        // between two values.
        func colorMixer(c1: UIColor, c2: UIColor, pct: CGFloat) -> Color {
            guard let cc1 = c1.cgColor.components else { return Color(c1) }
            guard let cc2 = c2.cgColor.components else { return Color(c1) }

            let r = (cc1[0] + (cc2[0] - cc1[0]) * pct)
            let g = (cc1[1] + (cc2[1] - cc1[1]) * pct)
            let b = (cc1[2] + (cc2[2] - cc1[2]) * pct)

            return Color(red: Double(r), green: Double(g), blue: Double(b))
        }

    }
}

答案 3 :(得分:0)

很遗憾,您无法为文本颜色设置动画。 对于可以设置动画的属性,必须在要设置动画的状态变化周围放置withAnimation,如下所示:

struct ContentView: View {

    @State var size: CGFloat = 25.0

    var body: some View {
        VStack {
            Rectangle()
                .foregroundColor(.green)
                .frame(width: size, height: size)
            Spacer()
            Button("Tap me") {
                withAnimation(.easeIn) {
                    self.size += 5.0
                }
            }
        }
    }
}