In my app, the user can set a timer for an activity. I want to show a simple progress bar that progresses every 1 second and stops (100%) at the end of the time interval of the timer.
example, think of the progressbar of a song being played which finishes on the length of the song.
I've read about TimerPublisher but am not sure how to implement it. Can someone help me out over here please?
Thanks Neerav
I implemented the timer this way... Only thing not working is invalidating the timer inspite of doing it on the main thread (same thread as firing the timer)
@IBAction func playOrPauseButtonPressed(_ sender: Any) {
var timer: Timer? = nil
timerInterval = habit?.timer
if !play!
{
if (timer == nil)
{
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
if self.secondsElapsed <= Int(self.timerInterval!)
{
self.secondsElapsed = self.secondsElapsed + 1
let progress = Float(self.secondsElapsed)/Float(self.timerInterval!)
DispatchQueue.main.async {
self.timerProgressView.setProgress(progress, animated: true)
}
}
else
{
DispatchQueue.main.async {
timer.invalidate()
self.playOrPauseButton.imageView?.image = UIImage(named: "Play")
}
self.play = false
self.secondsElapsed = 0
}
}
}
DispatchQueue.main.async {
self.playOrPauseButton.imageView?.image = UIImage(named: "Pause")
timer?.fire()
}
play = true
}
else
{
DispatchQueue.main.async {
self.playOrPauseButton.imageView?.image = UIImage(named: "Play")
timer?.invalidate()
}
play = false
}
}
Any help with that second part of invalidating the thread? I see the timer keeps firing and the progress view keeps updating.