Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created May 29, 2020 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khanlou/75e801cd5356d9e425649e9b99fb53cb to your computer and use it in GitHub Desktop.
Save khanlou/75e801cd5356d9e425649e9b99fb53cb to your computer and use it in GitHub Desktop.
//
// Spinner.swift
// spinner
//
// Created by Soroush Khanlou on 5/29/20.
// Copyright © 2020 Soroush Khanlou. All rights reserved.
//
import SwiftUI
struct ActivityIndicator: View {
@State var isAnimating = false
var animationDuration: Double = 1.2
private var pillCount = 12
var body: some View {
GeometryReader { geometry in
ZStack {
ForEach(0..<self.pillCount) { i in
Capsule()
.rotation(Angle(degrees: Double(30 * i)), anchor: UnitPoint(x: 0.5, y: 2))
.frame(width: geometry.size.width/18, height: geometry.size.width/5, alignment: .top)
.foregroundColor(Color(white: 0.5))
.opacity(self.isAnimating ? 0.1 : 1)
.offset(y: -geometry.size.height/3)
.animation(
Animation
.easeOut(duration: self.animationDuration)
.repeatForever(autoreverses: false)
.delay(Double(i) * self.animationDuration / Double(self.pillCount) - self.animationDuration)
)
}
}
}
.onAppear {
self.isAnimating = true
}
}
}
struct ActivityIndicator_Previews: PreviewProvider {
static var previews: some View {
ActivityIndicator()
.frame(width: 100, height: 100)
.border(Color.blue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment