UISlider with increments of 5

后端 未结 6 1170
离开以前
离开以前 2020-11-29 01:16

How do I have my UISlider go from 1-100 in increments of 5?

6条回答
  •  隐瞒了意图╮
    2020-11-29 01:33

    Add a target like this:

    slider.continuous = YES;
    [slider addTarget:self
          action:@selector(valueChanged:) 
          forControlEvents:UIControlEventValueChanged];
    

    And in the valueChanged function set the value to the closest value that is divisible by 5.

    [slider setValue:((int)((slider.value + 2.5) / 5) * 5) animated:NO];
    

    So if you need any interval other than 5 simply set it like so:

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

提交回复
热议问题