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];
This should accurately place the midpoint of the UISlider, regardless of the minimumValue or maximumValue being used.
Setting the Slider Position
In keeping with your code you could have:
Note: there is no real need for
self
when you're referencing theIBOutlet
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: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?
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.
If you want the start position of a UISlider (ideally in the middle) then
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