iOS how to make slider stop at discrete points

后端 未结 5 2169
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 06:21

I would like to make a slider stop at discrete points that represent integers on a timeline. What\'s the best way to do this? I don\'t want any values in between. It woul

5条回答
  •  無奈伤痛
    2020-12-13 06:55

    What I did for this is first set an "output" variable of the current slider value to an integer (its a float by default). Then set the output number as the current value of the slider:

    int output = (int)mySlider.value;
    mySlider.value = output;
    

    This will set it to move in increments of 1 integers. To make it move in a specific range of numbers, say for example, in 5s, modify your output value with the following formula. Add this between the first two lines above:

    int output = (int)mySlider.value;
    int newValue = 5 * floor((output/5)+0.5);
    mySlider.value = newValue;
    

    Now your slider "jumps" to multiples of 5 as you move it.

提交回复
热议问题