Skip to content

Instantly share code, notes, and snippets.

@Koshimizu-Takehito
Created May 11, 2024 09:59
Show Gist options
  • Save Koshimizu-Takehito/d3b2c4d78a77870c8d6f5a19ef3084d1 to your computer and use it in GitHub Desktop.
Save Koshimizu-Takehito/d3b2c4d78a77870c8d6f5a19ef3084d1 to your computer and use it in GitHub Desktop.
グラデーションアニメーション
import SwiftUI
extension [Color] {
static func rainbow(hue: Double = 0, count: Int) -> Self {
(0..<count).map { i in
var value = hue + Double(i) / Double(count)
value -= floor(value)
return Color(hue: value, saturation: 1/4, brightness: 1)
}
}
}
struct ContentView: View {
let date = Date()
var body: some View {
TimelineView(.animation) { context in
let time = context.date.timeIntervalSince(date)
Image(systemName: "apple.logo")
.resizable()
.aspectRatio(contentMode: .fit)
.foregroundStyle(
LinearGradient(
colors: .rainbow(hue: time/5, count: 256).reversed(),
startPoint: .topTrailing,
endPoint: .bottom
)
)
.animation(.spring(duration: 1.5)) { view in
view.scaleEffect(Int(time) % 2 == 0 ? 1 : 1.08)
}
}
.padding(40)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment