Created
May 11, 2024 09:59
-
-
Save Koshimizu-Takehito/d3b2c4d78a77870c8d6f5a19ef3084d1 to your computer and use it in GitHub Desktop.
グラデーションアニメーション
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 [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