Triple UISwitch

I want to create a custom UISwtich with three positions. Is it possible?

+4
source share
4 answers

You must use UISegmentedControl if you want a standard user interface or configure UISlider with a range of 2:

 slider.minimumValue = 0; slider.maximumValue = 2; slider.continuous = NO; 

And then set the minimumValueImage , maximumTrackImage and thumbImage appropriate images.

+10
source

Do not use embedded UISwitch. You will need to roll on your own.

+4
source

Why not use a UISegmentedControl?

+4
source

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]; } 
0
source

Source: https://habr.com/ru/post/1299543/


All Articles