Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment UISlider by 1 in range 1 to 100

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

like image 615
Krunal Avatar asked Oct 11 '12 07:10

Krunal


2 Answers

//Only generate update events on release
slider.continuous = NO;

//Round the value and set the slider
- (IBAction)sliderChange:(id)sender
{
    int rounded = sender.value;  //Casting to an int will truncate, round down
    [sender setValue:rounded animated:NO];

    NSLog(@"%f", sender.value);
}
like image 179
James Webster Avatar answered Oct 17 '22 08:10

James Webster


you could try this:

float RoundValue(UISlider * slider) {
  return roundf(slider.value * 2.0) * 1;
}
like image 8
Bazinga Avatar answered Oct 17 '22 08:10

Bazinga