Reputation: 195
I'm working on an project were I have put in a progress bar and then set a button to start the progressbar when it is pressed. The thing is that I want to have the progress in 30min when you pressed the startbutton, so I need help to set it to 30 minutes.
Here is my code to setup the progressbar and startbutton
the m-file
- (IBAction)StartButton:(id)sender {
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(moreProgress)
userInfo:Nil repeats:YES];
[super viewDidLoad];
//progressbar start when button pressed
}
and then i got this for the time
//selector progress time
- (void) moreProgress {
myProgressView.progress += 0.001;
}
Upvotes: 0
Views: 6851
Reputation: 149
// This extension will help you. I'had used this extension and it really work for me.
extension UIProgressView {
@available(iOS 10.0, *)
func setAnimatedProgress(progress: Float = 1, duration: Float = 1, completion: (() -> ())? = nil) {
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
DispatchQueue.main.async {
let current = self.progress
self.setProgress(current+(1/duration), animated: true)
}
if self.progress >= progress {
timer.invalidate()
if completion != nil {
completion!()
}
}
}
}
}
// Just call this extension with your UIProgressView like this
YOUR_PROGRESS_VIEW.setAnimatedProgress(duration: 100) { // Here 100 meaning 10 seconds
debugPrint("Done!")
}
Upvotes: 1
Reputation: 2482
1)Save the Current Time (You can save it to NSUserDefaults by example). You can do something like That:
NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
NSDate *currentDate = [NSDate date];
[user1 setObject:[NSNumber numberWithDouble:[currentDate timeIntervalSince1970]] forKey:@"time"];
[user1 synchronize];
2)schedule the call to your function like you did it
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(moreProgress)
userInfo:Nil repeats:YES];
3)Next in your function you need to update the progress view status with something like that:
//selector progress time
- (void) moreProgress {
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSDate *startTime = [NSDate dateWithTimeIntervalSince1970:[[user objectForKey:@"time"]doubleValue]];
NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];
float progress = (float)elapsed time / 30.0 * 60.0;
myProgressView.progress = progress;
if ((float)elapsedTime / 30 * 60 >= 1)
{
// The timer is end, you should stop it and do what you need
}
}
Note: It s not safe to expect that
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(moreProgress)
will fire up each seconds. It s not guaranteed and not precise.
Upvotes: 1
Reputation: 474
I think it's only a math problem.
First, myProgressView.progress can assume values from 0.0 to 1.0.
As you call moreProgress each second, you'll need to call it 1800 times for 30 minutes. Thus, myProgressView.progress should increase 1/1800 by second.
Change your code to:
- (void) moreProgress {
myProgressView.progress += 1.0/1800.0;
}
Hope it helps you! :)
Upvotes: 2