Last active
January 3, 2019 08:47
-
-
Save JaceTan/38e13b95e6d1433a717481f52879e495 to your computer and use it in GitHub Desktop.
This file contains 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
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