Skip to content

Instantly share code, notes, and snippets.

@mattyoung
Created May 16, 2022 13:34
Show Gist options
  • Save mattyoung/5b647a405500521ee97522b6e6a414e4 to your computer and use it in GitHub Desktop.
Save mattyoung/5b647a405500521ee97522b6e6a414e4 to your computer and use it in GitHub Desktop.
import SwiftUI
extension UnitPoint {
/// Compute the unit circle coordinates rotating at the center of UnitPoint fitted inside the bounding box
/// - Parameters:
/// - angle: angle of rotation vector of the coordinate
/// - factor: scaling factor of the unit circle
init(angle: Angle, scaling factor: Double = 1) {
self.init(x: (1 + factor * sin(angle.radians)) / 2, y: (1 + factor * cos(angle.radians)) / 2)
}
}
struct ConicGradientAngleAnimatableModifier: AnimatableModifier {
var animatableData: Double
init(angle degrees: Double) {
animatableData = degrees
}
func body(content: Content) -> some View {
let angle = Angle.degrees(animatableData)
content
.foregroundStyle(
.conicGradient(colors: [.red, .yellow, .green, .blue, .purple, .red],
center: .init(angle: angle, scaling: 0.7),
angle: angle)
)
}
}
struct GradientText: View {
@State private var animationAngle = 360.0
let text: String
var body: some View {
Text(text)
.modifier(ConicGradientAngleAnimatableModifier(angle: animationAngle))
.animation(.linear(duration: 3).repeatForever(autoreverses: false), value: animationAngle)
.onAppear { animationAngle = 0 }
}
init(_ text: String) {
self.text = text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment