72

How do I have my UISlider go from 1-100 in increments of 5?

Girish
  • 5,094
  • 4
  • 33
  • 53
Tyler29294
  • 721
  • 1
  • 5
  • 3

6 Answers6

165

Add a target like this:

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

And in the valueChanged function set the value to the closest value that is divisible by 5.

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

So if you need any interval other than 5 simply set it like so:

float interval = 5.0f;//set this
[slider setValue:interval*floorf((slider.value/interval)+0.5f) animated:NO];
Albert Renshaw
  • 15,644
  • 17
  • 92
  • 173
Tuomas Pelkonen
  • 7,559
  • 2
  • 28
  • 31
  • 1
    I feel honestly stupid. It's always the simple things that get me. – Michael Smith Feb 05 '12 at 20:26
  • 1
    Thanks, avoid me thinking to much about it ^^ I did do a slight change however: int increment = 10; [numberOfAppsInFutur setValue:((int)((numberOfAppsInFutur.value + increment/2.) / increment) * increment) animated:NO]; – Jason Rogers Apr 27 '12 at 14:26
  • In case it helps, there is a complete example [here](http://stackoverflow.com/a/8219848/168594). – zekel Jul 05 '14 at 11:11
65

A slightly more elegant solution in Swift could 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

}

This way you get steps of 5. You can read more about the setup in my post.

Victor
  • 4,652
  • 1
  • 22
  • 27
Jure
  • 2,783
  • 3
  • 21
  • 26
9

There is another way to active the stepper functionality. works in a general case

Example:

range of data is 0 - 2800 and I want the increments to be in 25 unit values. I would do the following

  1. set up the range of the slider to be 0 - (maxRange/unitsteps).
  2. when the value changed method runs use (int)slider.value * unitsteps .

in the example above my slider would be set in IB to range 0 - 112. and my code would be (int)slider.value * 25.

if you then have a text field that can be modified for direct input you would just do the opposite set the slider value to the [textfield.text intValue]/ unitsteps.

MB.
  • 669
  • 9
  • 26
7

For anyone looking for the 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 with the Assistant Editor (what I did ultimately)...

@IBAction func mySliderValueChanged(sender: UISlider) {
    //Set value to the nearest int
    sender.setValue(Float(roundf(sender.value)), animated: false)

}
G. Mass
  • 63
  • 1
  • 4
Justin Domnitz
  • 2,994
  • 24
  • 30
7

In swift 3, slider with increments +/- 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)")

}
-4
1. In storyboard mode, drag and drop a stepper and label.  the label will show the stepper values. 
  1. left click and drag both label and stepper to your view controller as outlets.

name label as "stepperValue"

  1. drag and drop stepper again making it a func- I called mine "func"

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

click the stepper and it will increment by 5

LARA Ann
  • 1
  • 1