Skip to content

Instantly share code, notes, and snippets.

@mralexhay
Forked from jordansinger/Sunset.swift
Last active March 13, 2021 23:51
Show Gist options
  • Save mralexhay/932b5331b5360f491666656f9aef4f42 to your computer and use it in GitHub Desktop.
Save mralexhay/932b5331b5360f491666656f9aef4f42 to your computer and use it in GitHub Desktop.
import SwiftUI
import UIKit
struct Sunset: View {
@State var backgroundColor = Color.blue
@State var sunSetted = false
let skyGradientDay = [Color(#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)), Color(#colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)), Color(#colorLiteral(red: 0.6254054655, green: 0.9221722869, blue: 0.9686274529, alpha: 1)), Color(#colorLiteral(red: 0.8345658875, green: 0.9221722869, blue: 0.9686274529, alpha: 1))]
let skyGradientNight = [Color(#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.4246178452, alpha: 1)), Color(#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.7072513187, alpha: 1)), Color(#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)), Color(#colorLiteral(red: 0.8078431487, green: 0.2959635248, blue: 0.6193262317, alpha: 1))]
let sunGradient = [Color.yellow, Color.orange]
let moonGradient = [Color(#colorLiteral(red: 0.5463274121, green: 0.04594885558, blue: 0.2291798294, alpha: 1)), Color(#colorLiteral(red: 0.5668596029, green: 0.1581864655, blue: 0.4246562719, alpha: 1))]
@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 (
ZStack {
Sky(gradient: skyGradientDay)
Sky(gradient: skyGradientNight)
.opacity(self.sunSetted ? 1 : 0)
}
)
.onAppear {
withAnimation(Animation.linear(duration: 12).repeatForever(autoreverses: true)) {
self.sunset()
}
}
}
func sunset() {
backgroundColor = .black
sunSetted = true
alignment = .bottom
}
}
struct Sky: View {
@State var gradient: [Color]
var body: some View {
LinearGradient(gradient: Gradient(colors: gradient), startPoint: .top, endPoint: .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)
}
}
struct Sunset_Previews: PreviewProvider {
static var previews: some View {
Sunset()
.previewLayout(PreviewLayout.sizeThatFits)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment