Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I restrict iOS slider value to an integer?

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)
}
like image 1000
Keyur Padalia Avatar asked Jan 05 '17 12:01

Keyur Padalia


People also ask

What is UISlider?

A UISlider is a control that allows the user to select a value from a range of values horizontally by dragging the thumb to the position desired. Some use cases where a UISlider is used: Changing the Volume. Changing the Brightness. Changing the current seek of the Video.

Can you set the current value of the slider beyond the range min max?

If you try to programmatically set a slider's current value to be below the minimum or above the maximum, it's set to the minimum or maximum instead. However, if you set the value beyond the range of the minimum or maximum in Interface Builder, the minimum or minimum values are updated instead.

How do you code a slider in Swift?

Enter Swift as Language and choose Next. Go to the Storyboard and drag a Slider to the main view and span its width to the width of the main View. Select the slider and go to the Attributes inspector. Under the slider section change the maximum value to 100 and change the current value to 0.

What is slider Xcode?

Overview. A slider consists of a “thumb” image that the user moves between two extremes of a linear “track”. The ends of the track represent the minimum and maximum possible values. As the user moves the thumb, the slider updates its bound value.


1 Answers

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.

like image 62
jboi Avatar answered Sep 29 '22 19:09

jboi