Using UISlider is a good approach. But you would also like to adjust the mechanics of your UISlider to be more similar to UISwitch. Ie, when you change your position not completely, then it should return to its original position.
Here is what I ended up doing (using part of the FelixLam answer):
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(screenRect.size.width*0.5-width/2, screenRect.size.height*0.95-height, width, height)]; slider.minimumValue = 0; slider.maximumValue = 2; slider.continuous = NO; slider.value = 1; [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
As well as...
- (void)sliderAction:(UISlider *)slider { float origValue = slider.value; [UIView beginAnimations:nil context:NULL]; if (slider.value<1.9 && slider.value>0.1) slider.value=1; else if (slider.value>1.9) slider.value=2; else slider.value=0; [UIView setAnimationDuration:0.2*fabs(slider.value-origValue)]; [UIView commitAnimations]; }
source share