Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios set start position and incrementation of a UISlider

Tags:

ios

uislider

Does anyone know how to set the start position of a UISlider (ideally in the middle) and also how to increment it in tens instead of ones?

Here's what I've got so far:

// Setup custom slider images
UIImage *minImage = [UIImage imageNamed:@"ins_blueTrack.png"];
UIImage *maxImage = [UIImage imageNamed:@"ins_whiteTrack.png"];
UIImage *tumbImage= [UIImage imageNamed:@"slide.png"];

minImage=[minImage stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];
maxImage=[maxImage stretchableImageWithLeftCapWidth:6.0 topCapHeight:0.0];

// Setup the  slider
[self.slider setMinimumTrackImage:minImage forState:UIControlStateNormal];
[self.slider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
[self.slider setThumbImage:tumbImage forState:UIControlStateNormal];

self.slider.minimumValue = 10;
self.slider.maximumValue = 180;
self.slider.continuous = YES;

int myVal = self.slider.value;
NSString *timeValue = [[NSString alloc] initWithFormat:@"%1d", myVal];

self.timeLabel.text = timeValue;

// Attach an action to sliding
[self.slider addTarget:self action:@selector(fxSliderAction) forControlEvents:UIControlEventValueChanged];
like image 325
Luke Batley Avatar asked May 22 '12 11:05

Luke Batley


2 Answers

Setting the Slider Position

In keeping with your code you could have:

self.slider.value = (90);

Note: there is no real need for self when you're referencing the IBOutlet from the same class.

If you want a truly dynamic way of setting the UISlider to the halfway mark, you would just divide your maximum value by 2, effectively halving it:

self.slider.value = (self.slider.maximumValue / 2);

Incrementing in Multiples of 10

For this I would suggest something slightly different to what you currently have. Instead of having a minimum of 10 and a maximum of 180, what about a minimum of 1 and a maximum of 18?

self.slider.minimumValue = 1;
self.slider.maximumValue = 18;

Every time you retrieve the value of the slider, just multiply it by 10. This way the slider moves to 18 different locations (as you wanted), and you always get a multiple of 10.

int trueSliderValue = self.slider.value * 10;
like image 76
Liam George Betsworth Avatar answered Oct 16 '22 23:10

Liam George Betsworth


If you want the start position of a UISlider (ideally in the middle) then

slider.value = (slider.maximumValue / 2);

if you want to increment it in 10's instead of ones then you should have that much maximum value. Now in slider value changed method check flooring and ceilling of current slider value then set it to slider.value = new value

like image 42
Paresh Navadiya Avatar answered Oct 16 '22 23:10

Paresh Navadiya