Skip to content

Instantly share code, notes, and snippets.

@Inncoder
Created December 14, 2020 15:38
Show Gist options
  • Save Inncoder/ad610a3329f5265336bc8f0c58f0d551 to your computer and use it in GitHub Desktop.
Save Inncoder/ad610a3329f5265336bc8f0c58f0d551 to your computer and use it in GitHub Desktop.
CircleSpinLoading
// Created by Inncoder.
// Copyright © 2020 Inncoder AS. All rights reserved.
//
import SwiftUI
struct CircleSpinLoading: View {
@State private var offset: CGFloat = 0
@State private var rotation: Double = 0
@State private var backgroundSize: CGFloat = 0
@State private var circleColor: Color = Color(red: 0.22, green: 0.24, blue: 0.31)
@State private var showBackground = false
var body: some View {
ZStack {
ZStack {
showBackground ? darkColor : Color.white
}.edgesIgnoringSafeArea(.all)
Circle()
.frame(width: backgroundSize, height: backgroundSize, alignment: .center)
.foregroundColor(showBackground ? Color.white : darkColor)
Circle()
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(circleColor)
.offset(x: 0, y: offset)
.rotationEffect(.degrees(rotation))
Circle()
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(circleColor)
.offset(x: 0, y: offset)
.rotationEffect(.degrees(rotation))
.rotationEffect(.degrees(180))
}
.onAppear() {
animate()
}
}
//MARK: - Drawing constants
let darkColor: Color = Color(red: 0.22, green: 0.24, blue: 0.31)
//MARK: - Functions
func animate() {
Timer.scheduledTimer(withTimeInterval: 1.7, repeats: true) { _ in
withAnimation(Animation.easeInOut(duration: 0.2)) {
offset = -100
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
withAnimation(Animation.easeInOut(duration: 0.5)) {
if showBackground {
rotation = 0
} else {
rotation = 360
}
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) {
withAnimation(Animation.easeInOut(duration: 0.2)) {
offset = 0
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.9) {
withAnimation(Animation.easeInOut(duration: 0.5)) {
if showBackground {
circleColor = darkColor
} else {
circleColor = .white
}
backgroundSize = UIScreen.main.bounds.height * 1.1
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.4) {
showBackground.toggle()
backgroundSize = 0
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment