0

In my current project I've created an app that utilizes the position of the UISlider in relation to a target value displayed to the user. I've got full functionality however I'm now trying to spice it up a bit.

Is it possible to set the UISlider to move from (minimum through maximum) than (maximum through minimum) back and forth on its own when the user enters the app rather than having them drag it?

Thoughts:

Possibly use a looping sequence?

Not looking for someone to code this for me I quite enjoy that part. Just looking for guidance.

nevan king
  • 112,709
  • 45
  • 203
  • 241

3 Answers3

0

UISlider has this method that allows you to programatically change the slider's value:

- (void)setValue:(float)value animated:(BOOL)animated

Be careful about using loops though, you don't want to block your main thread an make your app unresponsive.

Trumpetlick's answer provides good information on using an NSTimer, which should help you with this.

Community
  • 1
  • 1
Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
  • Pretty sure this wont provide the actual action the OP is looking for. He needs to also be able to run the action code (most likely) that is run when his user is actually using the slider, but the setValue is what he would want to use for setting the value of the actual slider itself. – trumpetlicks Feb 18 '14 at 00:31
  • It sounded to me like he doesn't need the user to drag the slider at all. – Connor Pearson Feb 18 '14 at 00:34
  • You have a good point, I guess he / she hasn't provided enough detail for us to know what else, if anything, he / she wants to do besides. The only thing that makes me wonder, is why would he / she have a slider in the design if they didn't want the user to interact with it, and have some action pursued when they do? All depends on what he intends to ACTUALLY do LOL – trumpetlicks Feb 18 '14 at 00:37
0

I think that in this case you would have to setup an NSTimer with a time that you wish per frame, separate out the action code within the slider into a routine that can be called by you, then within the timer action code, call your routine, and then set the slider position manually.

Look here for setting up an NSTimer

How do I use NSTimer?

The NSTimer will essentially allow you to setup any framerate you wish, AND keep it off of the MAIN thread.

Community
  • 1
  • 1
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
0

Further to the NSTimer answer, I wouldn't suggest using that, but CADisplayLink instead. Similar process, but instead of using NSTimer use CADisplayLink.

CADisplayLink synchronises with the screen refresh rate, so you can adjust the animation and slider value based on current frame, so the animation appears smooth.

http://patzearfoss.com/2011/09/02/more-cadisplaylink/

JamesSugrue
  • 14,891
  • 10
  • 61
  • 93