Increment UISlider by 1 in range 1 to 100

2019-01-12 03:45发布

I am new to iPhone,

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

    slider = [[UISlider alloc] init];
    [slider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
    [slider setBackgroundColor:[UIColor clearColor]];
    slider.minimumValue = 1;
    slider.maximumValue = 100;
    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
.
.
.

I want slider value as 1.0 , 2.0 , 3.0 and so on...

Any help will be appriciated.

7条回答
做自己的国王
2楼-- · 2019-01-12 04:05

you could try this:

float RoundValue(UISlider * slider) {
  return roundf(slider.value * 2.0) * 1;
}
查看更多
登录 后发表回答