Reputation: 97
I have a slider with 96 slots and I need to moves slider step by step from 0 to 95 in 60 seconds. Should I use NSTimer with interval (60/96) and 96 repeats or there is a better solution for this?
Upvotes: 1
Views: 626
Reputation: 4901
aTimer = [NSTimer timerWithTimeInterval:(1.0) target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:aTimer forMode: NSDefaultRunLoopMode];
- (void)timerFired:(NSTimer*)theTimer {
slider.maximumValue=totaltime;
if(slider.value ==totaltime)
{
[theTimer invalidate];
//terminate the loop
}
else
{
slider.value=slider.value+1;
}
}
//timer runs continuously run if condition is true
Upvotes: 1
Reputation: 5703
That's probably the best approach. The NSTimer
should behave fairly consistently at that interval, it only starts to get unreliable when calling it around every 1/10th second, or faster.
However, a bit of explanation in case it doesn't quite behave as you'd hoped:
It won't be perfect because the NSTimer
doesn't have it's tick event literally every interval. Rather, the NSTimer
is at the mercy of it's thread's run-loop, which may not get around to calling your @selector
method until a while after its interval has expired. Then combine that with calling for screen updates which are also not lock-step.
It's accuracy will mostly depend on what else you're doing in your run-loop... if there's not much going on in your device's little brain, then your slider should appear to move just as you'd hoped.
Edit: You may also consider an NSTimer with a longer interval, and use the UIView's animateWithDuration... methods to make it appear smooth?
Upvotes: 1