0

I'm new to Swift and I'm in trouble with a progressView. I launch my ProgressView timeBar with

self.timeBar.setProgress(10.0, animated: true)

Then I try to get the value of timeBar during the 10 seconds (between 0.0 and 1.0) :

print(timeBar.progress) 

But I get 1.0 or 0.0 anywhere the timeBar is along the progressView. I get 0.0 before the launch and 1.0 anywhere else.

How to catch the progress then ? Thanks ;)

Lostania
  • 145
  • 8
  • Are you updating your progress bar in the main thread dispatching it to the main? – xTwisteDx Oct 11 '20 at 15:12
  • Thanks for you answer. Frankly I don't know... I'm too new to know in which thread it is ! May be there is a way to know it ? Thanks again. – Lostania Oct 11 '20 at 15:13
  • Ok if you're not sure what GCD (Grand Central Dispatch) is, then we're going to need more context. What are you using to update your progress? A timer, a download, an upload, or something else? Depending on what's updating that progress bar will dictate our answer. – xTwisteDx Oct 11 '20 at 15:17
  • In fact I just lauched it using the self.timeBar.setProgress(10.0, animated: true). No Timer, no download nor upload. The bar appears animated during the 10 seconds without problem. But the value I get from the progress property is always 1.0. – Lostania Oct 11 '20 at 15:25

1 Answers1

2

Okay, in interface builder you can set the min-max for your progress bar. By default, it's 0.0 to 1.0 as a floating-point. You'll want to change that to 0.0 as min to 10.0 as max. Then I'll explain it a bit. That method .setProgress() simply sets it and animates it. There's no way to get the value it's currently at from that progress bar because the value is set immediately, then an animation runs, the animation does not change the value of the progress over time. The progress is first 0 then it's set to 1, then animates as if there were a value being set.

The proper way to handle this so that you can get the value is to use a timer, a download, upload, or some other method that indicates a true "Progress". For example progress from 1 second to 10 seconds, with each second being 0.1th of 10 or 10%. Here's a simple example. Another example might be a UIButton click that advances the progress' value by 1.0.

var timer = Timer()

func updateProgress() {
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerUpdate), userInfo: nil, repeats: false)
} 

@objc func timerAction(){
   if timeBar.progress >= 10.0 {
       timer.invalidate()
       return
   }


   timeBar.setProgress(timebar.progress + 1.0, animated: true)
}
xTwisteDx
  • 2,152
  • 1
  • 9
  • 25
  • Thank you a lot ! I thought there was a way to get the value ! It's strange it can't be done ! I'll use your way then :) Thanks again ! – Lostania Oct 11 '20 at 15:42