UISlider increments

て烟熏妆下的殇ゞ 提交于 2020-01-13 07:14:06

问题


I have a UISlider that slides with a range from 0.0 to 1.0.

Out of the box, the slider lets users slide with a really high number of fraction digits, but I only want my users to be able to increment/decrement by .001 for each "tick" up or down of the slider.

Is this possible to set up?

Thank you!


回答1:


How big is this slider? Even if you've got it spanning the full width of the screen in landscape mode, a change of 0.001 is only about half a pixel. It sounds like you just need to round the slider's value (behind the scenes) to the precision you're after.




回答2:


What about something extremely simple, for example: A 3 choices slider (with values {0, 1, 2}), with a IBAction and a IBOutlet connected to it.

-(float)roundValue:(float)value{

    if(value <=   0.5) return 0;
    if(value <=   1.5) return 1;
    return 2;
}

-(IBAction)mySliderChanged:(id)sender {

    float value = mySliderOutlet.value; // retrieve value via IBOutlet
    value = [self roundValue:value];

    // Update the slider to 'snap' in final position
    [[self mySliderOutlet] setValue:value animated:YES];
}



回答3:


This should do the trick:

- (void) getSliderValue:(UISlider *)paramSender{
    float increment = 0.001;
    float newValue = paramSender.value /increment;
    paramSender.value = floor(newValue) * increment;
    NSLog(@"Current value of slider is %f", paramSender.value);
}


来源:https://stackoverflow.com/questions/2458345/uislider-increments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!