Skip to content

Instantly share code, notes, and snippets.

@amannayak0007
Last active May 22, 2023 18:03
Show Gist options
  • Save amannayak0007/e8fb74843ba6b4ed64c3761777fd8a01 to your computer and use it in GitHub Desktop.
Save amannayak0007/e8fb74843ba6b4ed64c3761777fd8a01 to your computer and use it in GitHub Desktop.
Animated gradient background
import SwiftUI
struct ContentView: View {
var body: some View {
AnimatedBackground().edgesIgnoringSafeArea(.all)
.blur(radius: 50)
}
}
struct AnimatedBackground: View {
@State var start = UnitPoint(x: 0, y: -2)
@State var end = UnitPoint(x: 4, y: 0)
let timer = Timer.publish(every: 1, on: .main, in: .default).autoconnect()
let colors = [Color.blue, Color.red, Color.purple, Color.pink, Color.yellow, Color.green, Color.orange]
var body: some View {
LinearGradient(gradient: Gradient(colors: colors), startPoint: start, endPoint: end)
.animation(Animation.easeInOut(duration: 6).repeatForever())
.onReceive(timer, perform: { _ in
self.start = UnitPoint(x: 4, y: 0)
self.end = UnitPoint(x: 0, y: 2)
self.start = UnitPoint(x: -4, y: 20)
self.start = UnitPoint(x: 4, y: 0)
})
}
}
@amannayak0007
Copy link
Author

nimated gradient background in SwiftUI

@deinadmin
Copy link

looks beautiful

@Mmahtab10
Copy link

Smooth!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment