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];
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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With