1

I have a UISlider with value 0, minimum value 0 and maximum value 360 (set from the utilities area), and I want the value to increment by 15 (have an interval size of 15) with each swipe through VoiceOver, and have it snap with each swipe. e.g. swiping right once -> 15, twice -> 30 etc. I've been listening to what the current value on the slider is using VoiceOver.

I've already tried something similar to:

float interval = 5.0f;//set this
[slider setValue:interval*floorf((slider.value/interval)+0.5f) animated:NO]; 

source: https://stackoverflow.com/a/2519573/11693942

what I did based off of this code was:

angle = Double(15 * floorf((sender.value/15) + 0.5))
sender.setValue(Float(angle), animated: true)

but this makes my slider increment by 30.

Next, I tried:

angle = Double(15 * floorf((sender.value/30) + 0.5))
sender.setValue(Float(angle), animated: true)

which does increment my slider by 15, but then the slider gets stuck at 30. I can't increase the value, but I can decrease it. At this point VoiceOver keeps saying the value is 30, too.

I didn't get any crashes or error messages. The rest of my app still works, too.

jaosy
  • 11
  • 2

2 Answers2

0

Perhaps I misunderstood your question, but how about slider.value = slider.value + 15.0? Or slider.setValue(slider.value + 15.0, animated: true) if you want it animated.

That would be the easiest way to increase the value of a slider by 15.

Rengers
  • 14,911
  • 1
  • 36
  • 54
  • `slider.value = slider.value + 15.0` -> `slider.value += 15` – Alexander Jun 24 '19 at 20:52
  • Hi! thanks for your reply, that does make sense but I must have worded my question wrong because I actually meant that I wanted my slider to snap to increments of 15 (interval size of 15) with each swipe. I have edited my question to reflect that now :) I tried what you suggested and the first swipe goes to 15, but the next one is 66 and then 117 degrees, so the interval size isn't what I want – jaosy Jun 25 '19 at 15:23
0

Resolved with the help of a teammate :) ! Closing.

@IBAction func angleSliderChanged(_ sender: UISlider) {
        let roundingNumber: Float = (interval/2.0)
        angle = Double(sender.value)
        sender.accessibilityValue = "\(Int(angle)) degrees"

    sender.setValue(interval*floorf(((sender.value+roundingNumber)/interval)), animated:false)
    }
jaosy
  • 11
  • 2