Skip to content

Instantly share code, notes, and snippets.

@indyfromoz
Forked from jordansinger/Sunset.swift
Created July 17, 2020 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indyfromoz/135bc41cda560cd9ef72734716d01367 to your computer and use it in GitHub Desktop.
Save indyfromoz/135bc41cda560cd9ef72734716d01367 to your computer and use it in GitHub Desktop.
import SwiftUI
import PlaygroundSupport
struct Sunset: View {
@State var backgroundColor = Color.blue
@State var sunSetted = false
let sunGradient = [Color.yellow, Color.orange]
let moonGradient = [Color.gray, Color.black]
@State var alignment = Alignment.top
var body: some View {
ZStack(alignment: alignment) {
ZStack {
Sun(gradient: sunGradient)
Sun(gradient: moonGradient)
.opacity(self.sunSetted ? 1 : 0)
}
GeometryReader { geometry in
VStack {
Spacer()
Blur(style: .systemUltraThinMaterial)
.frame(height: geometry.size.height / 2)
}
}
}
.background(backgroundColor)
.cornerRadius(24)
.onAppear {
withAnimation(Animation.linear(duration: 12).repeatForever(autoreverses: true)) {
self.sunset()
}
}
}
func sunset() {
backgroundColor = .black
sunSetted = true
alignment = .bottom
}
}
struct Sun: View {
@State var gradient: [Color]
var body: some View {
Circle()
.fill(
LinearGradient(gradient: Gradient(colors: gradient), startPoint: .top, endPoint: .bottom)
)
.frame(width: 192, height: 192)
.padding()
}
}
struct Blur: UIViewRepresentable {
var style: UIBlurEffect.Style
func makeUIView(context: Context) -> UIVisualEffectView {
return UIVisualEffectView(effect: UIBlurEffect(style: style))
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
uiView.effect = UIBlurEffect(style: style)
}
}
PlaygroundPage.current.setLiveView(Sunset())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment