Skip to content

Instantly share code, notes, and snippets.

@JaceTan
Last active January 3, 2019 08:47
Show Gist options
  • Save JaceTan/38e13b95e6d1433a717481f52879e495 to your computer and use it in GitHub Desktop.
Save JaceTan/38e13b95e6d1433a717481f52879e495 to your computer and use it in GitHub Desktop.
import Async
import UIKit
// UI/UX Elements
@IBOutlet weak var animatingLabel: UILabel!
@IBOutlet weak var statusLabel: UILabel!
// Constants and Variables
private var animatingTimer: Timer?
private let maximumLoadingString = "..."
private let miniumLoadingString = "."
func invalidateAnimatingTimer() {
animatingTimer?.invalidate()
animatingTimer = nil
}
@objc func performAnimatingLabel() {
guard let currentStatusMessage = animatingLabel.text else { return }
switch currentStatusMessage.components(separatedBy: miniumLoadingString).count - 1 {
case 1, 2: animatingLabel.text = currentStatusMessage + miniumLoadingString
default: animatingLabel.text = currentStatusMessage.replace(target: maximumLoadingString, withString: miniumLoadingString)
}
}
func updateStatus(with message: String) {
Async.main { [weak self] in
guard let self = self else { return }
self.animatingLabel.text = self.maximumLoadingString
self.statusLabel.text = message
self.invalidateAnimatingTimer()
self.animatingTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.performAnimatingLabel), userInfo: nil, repeats: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment