3

I've created a simple tip calculator for practice and have implemented UISliders for the "tip percentage" and "Split" amounts.

The Split amount divides the total bill amount by the value of the Split UISlider obviously, but the problem I'm having is that it is getting continuous updates of the slider... I've unchecked "Continuous updates" from Attributes inspector and have even added the uislider.continuousupdates = false to the code but no luck.

How do I get the UISlider to snap to the nearest whole value?

Any help here is appreciated.

Qiu
  • 5,651
  • 10
  • 49
  • 56
hyunsbuns
  • 45
  • 2
  • I create a [custom slide](http://stackoverflow.com/questions/30530286/how-to-customize-uislider-value-in-swift/30530825#30530825) and post in another question here at stackoverflow, have a look it maybe what you are looking for. – Icaro Jun 03 '15 at 05:44

1 Answers1

1

You could always convert the slider value to an Int in its IBOutlet function and then call whatever other method from there, or change the slider's value to that Int if that's what you're trying to do. Not sure if that's what you're looking for but:

let constantValue = 8 // Don't change this

@IBAction func slider(_ sender: UISlider) // Connect your UISlider to this
{
    var sliderValue = Int(sender.value) // Get the nearest Int value from your slider
    // Just a quick print statement to make sure we're getting the right number from the slider
    print("\(constantValue)==D, Slider's nearest whole number: \(sliderValue)")
    sender.value = sliderValue // This will set the slider's value to the nearest Int
    exampleFunction(sliderValue) // Optionally call another example function from here using the new whole number from the slider
}
Justin Lennox
  • 147
  • 15