ios set start position and incrementation of a UIS

2019-06-23 23:32发布

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];

标签: ios uislider
3条回答
▲ chillily
2楼-- · 2019-06-23 23:45

This should accurately place the midpoint of the UISlider, regardless of the minimumValue or maximumValue being used.

slider.minimumValue = -10.0
slider.maximumValue = 1000.0
let trueMidPoint = ((slider.maximumValue-0)-(0-slider.minimumValue))/2
slider.value = trueMidPoint
查看更多
Evening l夕情丶
3楼-- · 2019-06-23 23:58

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;
查看更多
看我几分像从前
4楼-- · 2019-06-24 00:02

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

查看更多
登录 后发表回答