Created
August 19, 2025 11:53
-
-
Save m1ckc3s/7170d4d6c5b785a8a5cc014025159b09 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
| // | |
| // StarrySkyView.swift | |
| // mystiks | |
| // | |
| // Created by mick on 8/19/25. | |
| // | |
| import SwiftUI | |
| struct LoadingParticle: Identifiable { | |
| let id = UUID() | |
| let color: Color | |
| var position: CGPoint | |
| var size: CGFloat | |
| var opacity: Double | |
| var scale: CGFloat | |
| } | |
| struct StarrySkyView: View { | |
| @State private var particles: [LoadingParticle] = [] | |
| private let particleCount = 100 | |
| var body: some View { | |
| GeometryReader { geometry in | |
| ZStack { | |
| ForEach(particles) { particle in | |
| Circle() | |
| .foregroundColor(particle.color) | |
| .opacity(particle.opacity) | |
| .frame(width: particle.size, height: particle.size) | |
| .scaleEffect(particle.scale) | |
| .position(particle.position) | |
| .onAppear { | |
| animateParticle(particle) | |
| } | |
| } | |
| } | |
| .onAppear { | |
| particles = generateParticles(in: geometry.size) | |
| } | |
| } | |
| } | |
| private func generateParticles(in size: CGSize) -> [LoadingParticle] { | |
| var particles: [LoadingParticle] = [] | |
| for _ in 0..<particleCount { | |
| let x = CGFloat.random(in: 0...size.width) | |
| let y = CGFloat.random(in: 0...size.height) | |
| let size = CGFloat.random(in: 1...1.5) | |
| particles.append( | |
| LoadingParticle( | |
| color: .white, | |
| position: CGPoint(x: x, y: y), | |
| size: size, | |
| opacity: Double.random(in: 0.0...1.0), | |
| scale: CGFloat.random(in: 0.5...2) | |
| ) | |
| ) | |
| } | |
| return particles | |
| } | |
| private func animateParticle(_ particle: LoadingParticle) { | |
| guard let index = particles.firstIndex(where: { $0.id == particle.id }) else { | |
| return | |
| } | |
| withAnimation(createAnimation()) { | |
| particles[index].opacity = Double.random(in: 0.1...1.0) | |
| particles[index].scale = CGFloat.random(in: 0.1...2.0) | |
| particles[index].position.x += CGFloat.random(in: -7...7) | |
| particles[index].position.y += CGFloat.random(in: -7...7) | |
| } | |
| DispatchQueue.main.asyncAfter(deadline: .now() + Double.random(in: 1...2)) { | |
| animateParticle(particles[index]) | |
| } | |
| } | |
| private func createAnimation() -> Animation { | |
| Animation.easeInOut(duration: Double.random(in: 1...2)) | |
| } | |
| } | |
| struct StarrySkyView_Previews: PreviewProvider { | |
| static var previews: some View { | |
| ZStack { | |
| Color.black.ignoresSafeArea() | |
| StarrySkyView() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment