0

Possible Duplicate:
Increment UISlider by 1 in range 1 to 100

I am new to iPhone,

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
.
.
.

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

Any help will be appriciated.

Community
  • 1
  • 1
Krunal
  • 6,440
  • 21
  • 91
  • 155

1 Answers1

10

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.

You can round like this:

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

(Thanks @koki)

jrturton
  • 118,105
  • 32
  • 252
  • 268