Search code examples
objective-ciosnstimeruislidernstimeinterval

Moving slider during a certain time


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?


Solution

  • 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?