How can I restrict iOS slider value to an integer?
Asked Answered
A

8

15

I have a horizontal UISlider to control the volume from 1 to 16 as min and max value, but it's returning float values when I do print().

How can I restrict the UISlider value to an integer?

@IBOutlet var ringValue: UISlider!

@IBAction func ringVolumeSliderChange(_ sender: UISlider)
{
    print(sender.value)
}
Algeria answered 5/1, 2017 at 12:42 Comment(2)
Can you please care to add reason of unaccepting the answer, so i can improve my answer.Raker
The feature is not built in unfortunately. if you want a true clicky integer slider, you'd have to build from scratch.Humeral
S
7
@IBAction func SliderValueChanged(_ sender: UISlider) {
    showValueSlider.text = String(format: "The value is:%i",Int(sender.value))
}
Stern answered 20/7, 2018 at 10:17 Comment(1)
While this code may answer the question, providing information on how and why it solves the problem improves its long-term value.Pedestrian
R
16

value property of UISlider is of Float type so you cannot change that but you can convert it to Int using Int(sender.value).

@IBAction func ringVolumeSliderChange(_ sender: UISlider) {
    print(Int(sender.value))
}
Raker answered 5/1, 2017 at 12:44 Comment(1)
you can also set the value of the slider to the int-converted value so your slider isn't freely changeble. But it kind of snaps to the value you choose. It would look like this in the IBAction let iSliderValue = Int(sender.value) sender.value = Float(iSliderValue)Mayamayakovski
C
14

If you want your Slider to position only at the steps do:

@IBAction func ringVolumeSliderChange(_ sender: UISlider)
{
    sender.setValue(sender.value.rounded(.down), animated: true)
    print(sender.value)
}

In this example, I assume you've set min and max values. Now the slider jumps from position to position as the user slides around.

Crosscut answered 5/1, 2017 at 13:11 Comment(0)
S
7
@IBAction func SliderValueChanged(_ sender: UISlider) {
    showValueSlider.text = String(format: "The value is:%i",Int(sender.value))
}
Stern answered 20/7, 2018 at 10:17 Comment(1)
While this code may answer the question, providing information on how and why it solves the problem improves its long-term value.Pedestrian
I
5

To make the slider more smoothly and take one result at a time (when the user stop sliding) you can use this line inside viewDidLoad :

ringValue.isContinuous = false

Then you can choose how you will handle the result from one of the other answers. I liked jboi's answer so it will take discrete values.

Inflection answered 5/1, 2017 at 13:55 Comment(0)
H
2

I have a horizontal UISlider to control the volume from 1 to 16 as min and max value...

By default, the minimum value of value property is 0.0 and the maximum is 1.0.

If you want to get an integer number from 1 to 16 based on the slider value, you should do what @NiravD suggested with extra -pretty simple- maths :)

@IBAction func ringVolumeSliderChange(_ sender: UISlider) {
    print(Int((sender.value * 15).rounded()) + 1)
}

Letting it as print(Int(sender.value)), it prints 1 iff the slider reached its maximum value (1.0), all other values are less that 1.0 will printed as 0.

Hidrosis answered 5/1, 2017 at 13:28 Comment(3)
Note that Int by Double initializer truncates the fractional part of the number (if it doesn't overflow, which it wont here), so e.g. for sender.value * 15 equal to, say, 1.7, initializer will truncate to 1 (but this should probably be 2). You might consider rounding the calculated Double value prior to Int initialization (e.g. Int((sender.value * 15).rounded()) + 1)Cocotte
Or even lroundf(sender.value * 15) + 1 :)Swordbill
@dfri thanks for the note, I edited my answer based on what you mentionedHidrosis
L
0
var slideValue: String = "0"

@objc private func didSliderValueChanged(_ sender: UISlider) {
    let formattedValue = String(format: "%.0f", sender.value)
    
    if let floatValue = Float(formattedValue) {
        sender.value = floatValue
    }
        
    guard formattedValue != slideValue else {
        return
    }

    //This part is optional, and can make slider more interactive
    UIImpactFeedbackGenerator(style: .medium).impactOccurred()

    slideValue = formattedValue
}
Leonoreleonsis answered 5/5, 2021 at 13:38 Comment(1)
Welcome to SO. This code may solve the problem, but your answer will be of greater help to other users if you add explanation as to how it solves the problem.Bookrack
V
0

SwiftUI

@State private var diameter: Double = 10
var body: some View {
     HStack {
       Slider(value: $diameter, in: 0...100)
       Text("\(Int(diameter))")
     } 
}
V1 answered 27/5, 2021 at 6:50 Comment(4)
@State private within the body? Are you sure this code works?Nettle
Can you make the slider jump to discrete values as in jboi's answer?Malocclusion
@Malocclusion SwiftUI has a Slider initializer --> Slider(value: , in: , step: , onEditingChanged: minimumValueLabel: , maximumValueLabel: , label: )V1
@V1 Cristik's comment came before your edit to move diameter.Malocclusion
F
-2

You can try this

@IBOutlet var ringValue: UISlider!

@IBAction func ringVolumeSliderChange(_ sender: UISlider)
{
   print(String(format: "%.0f", sender.value))
}
Fidole answered 21/11, 2023 at 11:38 Comment(1)
Please do not post duplicate answers.Vertu

© 2022 - 2024 — McMap. All rights reserved.