UISlider with stops every quarter.

have a UISlider setup to stop every time it reaches a round number. Here's the code:

// Slider
    @IBAction func sizerSliderAction(sender: UISlider) {
       
        // Make slider stop every round number size.
        sender.setValue(Float(lroundf(sizerSlider.value)), animated: true)
       
        // Define the current value as float.
        let currentValue = Float(sender.value)
       
        // Format slider value with two decimal points.
        let decimalValue = NSString(format: "%.2f", currentValue)
       
        // Output slider value in our label.
        sizerLabel.text = "\(decimalValue)"
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        print("Sizer View Controller Loaded.")
    }

I want the slider to stop every time it reaches a quarter size as follows:


5.00 -> 5.25 -> 5.50 -> 5.75 -> 6.00 etc...


How can I do this?

Answered by junkpile in 76099022

OK. I just typed it in the browser based on your code. Maybe use roundf instead of lroundf. The gist of it is: multiply your value by 4, then round it, then divide by 4.

sender.setValue(Float(lroundf(sizerSlider.value * 4.0) / 4.0), animated: true)

Thanks for the quick reply. That doesn't complie:


Binary operator '/' cannot be applied to operands of type 'int' and 'double'

Accepted Answer

OK. I just typed it in the browser based on your code. Maybe use roundf instead of lroundf. The gist of it is: multiply your value by 4, then round it, then divide by 4.

That's it. Thank you!

Ten years later it is now trivial to do this .. https://stackoverflow.com/a/79573572/294884

UISlider with stops every quarter.
 
 
Q