本文介绍了在0到2.0范围内将UISlider增加0.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone新手,

I am new to iPhone,

如何让我的UISlider从0增加到2,增量为0.2?

How do I have my UISlider go from 0 to 2 in increments of 0.2?

slider = [[UISlider alloc] init];
[slider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = 2.0;
slider.continuous = YES;
slider.value = 0.0;

- (IBAction)sliderChange:(id)sender{
    NSLog(@"slider.value=%f",slider.value);
}

当我滑动我的日志显示...

When i slide my log shows...

slider.value = 1.000000
slider.value = 1.123440
slider.value = 1.234550
slider.value = 1.345670
slider.value = 1.567890
.
.
.

我希望滑块值为0.0,0.2,0.4等......

I want slider value as 0.0 , 0.2 , 0.4 and so on...

任何帮助都将适用。

推荐答案

在sliderChanged方法中,取值滑块,将其四舍五入到最接近的0.2,然后将滑块的值设置为此舍入值。这会将滑块滑动到0.2增量。

In your sliderChanged method, take the value from your slider, round it to the nearest 0.2, then set the value of your slider to this rounded value. This will snap the slider thumb to 0.2 increments.

你可以这样舍入:

float roundedValue = roundf(value / 0.2f) * 0.2f;

(谢谢@koki)

这篇关于在0到2.0范围内将UISlider增加0.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:42