Skip to content

Instantly share code, notes, and snippets.

@Inncoder
Last active August 27, 2020 17:41
Show Gist options
  • Select an option

  • Save Inncoder/17d6a89ad77f4ae82d31347465868010 to your computer and use it in GitHub Desktop.

Select an option

Save Inncoder/17d6a89ad77f4ae82d31347465868010 to your computer and use it in GitHub Desktop.
Typing indicator
//
// TypingIndicator.swift
// TypingIndicator
//
// Created by Inncoder on 19/08/2020.
// Copyright © 2020 Inncoder AS. All rights reserved.
//
import SwiftUI
struct TypingIndicator: View {
@State private var numberOfTheAnimationgBall = 3
var body: some View {
HStack(alignment: .firstTextBaseline) {
ForEach(0..<3) { i in
Capsule()
.foregroundColor((self.numberOfTheAnimationgBall == i) ? .blue : Color(UIColor.darkGray))
.frame(width: self.ballSize, height: (self.numberOfTheAnimationgBall == i) ? self.ballSize/3 : self.ballSize)
}
}
.animation(Animation.spring(response: 0.5, dampingFraction: 0.7, blendDuration: 0.1).speed(2))
.onAppear {
Timer.scheduledTimer(withTimeInterval: self.speed, repeats: true) { _ in
var randomNumb: Int
repeat {
randomNumb = Int.random(in: 0...2)
} while randomNumb == self.numberOfTheAnimationgBall
self.numberOfTheAnimationgBall = randomNumb
}
}
}
// MAKR: - Drawing Constants
let ballSize: CGFloat = 20
let speed: Double = 0.3
}
struct TypingIndicator_Previews: PreviewProvider {
static var previews: some View {
TypingIndicator()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment