Skip to content

Instantly share code, notes, and snippets.

@twstokes
Created August 9, 2020 23:37
Show Gist options
  • Save twstokes/77c253d1fdb33863ddb181a983c9b3f8 to your computer and use it in GitHub Desktop.
Save twstokes/77c253d1fdb33863ddb181a983c9b3f8 to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// Moon
//
// Created by Tanner W. Stokes on 8/9/20.
//
import SwiftUI
struct ContentView: View {
@State var day = true
let sunColor = Color(red: 245 / 255, green: 129 / 255, blue: 66 / 255)
var body: some View {
ZStack {
Orbits()
.scale(day ? 0.85 : 0.7)
.rotation(day ? Angle(radians: 0) : Angle(radians: .pi / 3))
.fill(sunColor)
Circle()
.scale(day ? 0.6 : 0.85)
.fill(day ? sunColor : Color.yellow)
Circle()
.scale(0.85)
.fill(day ? Color.white : Color.black)
.opacity(day ? 0.0 : 1.0)
.offset(x: day ? 0 : 60.0, y: day ? 0 : -60.0)
}
.background(day ? Color.white : Color.black)
.ignoresSafeArea()
.onTapGesture {
day.toggle()
}
.animation(.spring(response: 1, dampingFraction: 1, blendDuration: 0.5))
}
}
struct Orbits: Shape {
func path(in rect: CGRect) -> Path {
let r: CGFloat = 0.25 // relative to unit circle
var path = Path()
for rad in stride(from: 0.0, to: .pi*2, by: .pi/4) {
let origin = CGPoint(x: cos(rad), y: sin(rad))
let rect = CGRect(origin: origin, size: .init(width: r, height: r))
path.addEllipse(in: rect)
}
let scaleFactor = min(rect.width, rect.height) / 2
let shiftFactor = scaleFactor * r / 2
return path
.applying(.init(scaleX: scaleFactor, y: scaleFactor))
.applying(.init(translationX: rect.midX - shiftFactor, y: rect.midY - shiftFactor))
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@twstokes
Copy link
Author

twstokes commented Aug 9, 2020

SunMoon

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