Created
March 3, 2024 05:00
-
-
Save Koshimizu-Takehito/66c22f4f0e144ebee2bb40b232ac950f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
struct LoadingView: View { | |
@State var theta: Double = 0 | |
let radius: Double = 60 | |
var body: some View { | |
ZStack { | |
ForEach(0..<8) { index in | |
let offset = ((2 * .pi) * Double(index)) / 8 | |
let theta = theta + offset | |
Dot(radius: radius, theta: theta, offset: offset) | |
} | |
} | |
.task { | |
withAnimation(.linear(duration: 2).repeatForever(autoreverses: false)) { | |
theta = 2 * .pi | |
} | |
} | |
} | |
} | |
struct Dot: View, Animatable { | |
var radius, theta, offset: Double | |
var animatableData: Double { get { theta } set { theta = newValue } } | |
var body: some View { | |
let scale = 0.5 + (abs((theta - .pi) | |
.remainder(dividingBy: 2 * .pi)))/(2 * .pi) | |
let color = color(value: scale) | |
Circle() | |
.frame(width: 30) | |
.scaleEffect(x: scale, y: scale) | |
.offset( | |
x: radius * (cos(theta + offset) + cos(offset)), | |
y: radius * (sin(theta + offset) + sin(offset)) | |
) | |
.foregroundStyle(color) | |
.shadow(color: color.opacity(0.5), radius: 10, y: 15 * scale) | |
} | |
func color(value: Double) -> Color { | |
Color(hue: value, saturation: 1, brightness: 1) | |
} | |
} | |
#Preview { | |
LoadingView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment