Lostania
Lostania

Reputation: 145

ProgressView progress stuck at 1.0 or 0.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 ;)

Upvotes: 0

Views: 1459

Answers (1)

xTwisteDx
xTwisteDx

Reputation: 2472

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)
}

Upvotes: 2

Related Questions