Skip to content

Instantly share code, notes, and snippets.

@Exey
Created September 15, 2021 16:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Exey/3d8c3c5bc76e0d298d88e8b933d191a9 to your computer and use it in GitHub Desktop.
Save Exey/3d8c3c5bc76e0d298d88e8b933d191a9 to your computer and use it in GitHub Desktop.
ColorAnimation.swift
private struct ColorAnimation: AnimatableModifier {
var animatableData: Double
private let rgbaPair: [(Double, Double)]
init(from: Color, to: Color, percentToColor: Double) {
animatableData = percentToColor
let fromComponents = UIColor(from).cgColor.components!
let toComponents = UIColor(to).cgColor.components!
rgbaPair = Array(zip(fromComponents.map(Double.init), toComponents.map(Double.init)))
}
func body(content: Content) -> some View {
content
.foregroundColor(mixedColor)
}
// This is a very basic implementation of a color interpolation between two values.
private var mixedColor: Color {
let rgba = rgbaPair.map { $0.0 + ($0.1 - $0.0) * animatableData }
return Color(red: rgba[0], green: rgba[1], blue: rgba[2], opacity: rgba[3])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment