UISlider in increments of 5

How do I make a UISlider UISlider from 1-100 in increments of 5 ?

+62
ios iphone uislider
Mar 25 '10 at 21:16
source share
6 answers

Add a goal like this:

 slider.continuous = YES; [slider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; 

And in the valueChanged function, set the value to the nearest value, which is divided by 5.

 [slider setValue:((int)((slider.value + 2.5) / 5) * 5) animated:NO]; 



Therefore, if you need an interval other than 5, just set it like this:

 float interval = 5.0f;//set this [slider setValue:interval*floorf((slider.value/interval)+0.5f) animated:NO]; 
+154
Mar 25 '10 at 21:32
source share

A slightly more elegant solution in Swift might be something like this

 let step: Float = 5 @IBAction func sliderValueChanged(sender: UISlider) { let roundedValue = round(sender.value / step) * step sender.value = roundedValue // Do something else with the value } 

So you get steps 5. You can read more about customization in my post .

+43
Nov 16 '15 at 20:08
source share

There is another way to activate the functionality of a stepper device. works in general

Example:

the data range is 0 - 2800, and I want the increments to be 25 units. I would do the following

  • set the slider range to 0 - (maxRange / unitsteps).
  • when the value of the changed method starts using (int) slider.value * unitsteps.

in the above example, my slider will be set to IB up to a range from 0 to 112. and my code will be (int) slider.value * 25.

if you have a text field that can be changed for direct input, you would just do the opposite slider value in [textfield.text intValue] / unitsteps.

+7
Oct 7 '11 at 3:15 a.m.
source share

For those looking for Swift syntax ...

 override func viewDidLoad() { super.viewDidLoad() mySlider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged) } func sliderValueDidChange(sender:UISlider!) { //Set value to the nearest 5... print((Float)((Int)((sender.value + 2.5) / 5) * 5)) sender.setValue((Float)((Int)((sender.value + 2.5) / 5) * 5), animated: false) } 

Alternatively, just create an Action function using the helpers editor (which I did in the end) ...

 @IBAction func mySliderValueChanged(sender: UISlider) { //Set value to the nearest int sender.setValue(Float(roundf(sender.value)), animated: false) } 
+6
Jul 20 '15 at 17:29
source share

In swift 3, the slider in increments of +/- 7:

 @IBOutlet weak var sldComponent: UISlider! 

...

 sldComponent.addTarget(self, action: #selector(_sldComponentChangedValue),for: .valueChanged) 

...

 func _sldComponentChangedValue(sender: UISlider!) { self.sldComponent.setValue((round(sender.value / 7) * 7), animated: false) print("\(sender.value)") } 
+5
Apr 13 '17 at 12:31 on
source share
 1. In storyboard mode, drag and drop a stepper and label. the label will show the stepper values. 
  1. left click and drag the shortcut and pedometer to the controller of your view as outputs.

name label as "stepperValue"

  1. drag the jog switch again, making it func- i called my "func"

@IBAction func inc (sender: AnyObject) {
stepperValue.text = "(Int (stepper.value) * 5)"

click the pedometer and it will increase by 5

-four
Apr 26 '16 at 16:18
source share



All Articles