Created
February 8, 2025 18:51
-
-
Save andrewgleave/c773f370f8eae7d98d65dbb178456638 to your computer and use it in GitHub Desktop.
Complex Gradient Animation in SwiftUI - based on https://hturan.com/writing/complex-numbers-glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension View { | |
func complexGradientEffect(angle: Double, offset: Double) -> some View { | |
let function = ShaderFunction( | |
library: .default, | |
name: "complexGradient" | |
) | |
let shader = Shader(function: function, arguments: [ | |
.boundingRect, | |
.float(angle), | |
.float(offset) | |
]) | |
return self.colorEffect(shader, isEnabled: true) | |
} | |
} | |
struct ExampleComplexGradientView: View { | |
let start = Date() | |
var body: some View { | |
GeometryReader { geometry in | |
ZStack { | |
TimelineView(.animation) { context in | |
let elapsed = Date().timeIntervalSince(start) | |
let angle = sin(elapsed * 0.10) * .pi | |
Rectangle() | |
.complexGradientEffect(angle: angle, offset: 0.0) | |
.ignoresSafeArea() | |
.frame(width: geometry.size.width, height: geometry.size.height) | |
} | |
} | |
} | |
} | |
} | |
#Preview { | |
ExampleComplexGradientView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment