Ios sets the starting position and gain of UISlider

Does anyone know how to set the starting position of UISlider (ideally in the middle), and also how to increase it in tens instead of units?

Here is what I have 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]; 
+4
source share
3 answers

Adjust slider position

According to your code, you could:

 self.slider.value = (90); 

Note: There is no real need for self when you reference an IBOutlet from the same class.

If you want a truly dynamic way to set the UISlider sign halfway, you simply divide your maximum value by 2, effectively halve it:

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

Increment in multipliers 10

For this, I would suggest a little different from what you have. Instead of having a minimum of 10 and a maximum of 180, at least a minimum of 1 and a maximum of 18?

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

Each time you extract the value of the slider, simply multiply it by 10. Thus, the slider moves to 18 different locations (as you like), and you always get a multiple of 10.

 int trueSliderValue = self.slider.value * 10; 
+3
source

If you want the starting position of UISlider (ideally in the middle), then

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

if you want to increase it by 10 instead of units, then you should have such a maximum value. Now in the slider the method of checking the floors and drowning of the current value of the slider has changed, and then set it to slider.value = new value

0
source

This should accurately position the midpoint of the UISlider, regardless of the minimum value used or the maximum value.

 slider.minimumValue = -10.0 slider.maximumValue = 1000.0 let trueMidPoint = ((slider.maximumValue-0)-(0-slider.minimumValue))/2 slider.value = trueMidPoint 
0
source

Source: https://habr.com/ru/post/1413732/


All Articles