Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// 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

This comment has been minimized.

Copy link
Owner Author

@twstokes twstokes commented Aug 9, 2020

SunMoon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.