Animate Color with Animatable

How to animate color changes with Color structure and Animatable protocol? AnimatableData supports floating point types only.


(Edited.)

Replies

If you are controlling the color with a State or something similar, all you need to do is make sure you change the state variable inside a withAnimation block.


struct ColorView: View {
    @State var toggleable = true
    var body: some View {
        VStack {
            Text("Hello World")
                .background(Color(toggleable ? UIColor.blue : UIColor.red))
            Button(action: {
                withAnimation {
                    self.toggleable.toggle()
                }
            }) {
                Text("toggle")
            }
        }
    }
}