Triple UISwitch

吃可爱长大的小学妹 提交于 2019-12-07 08:34:26

问题


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


回答1:


You should be using UISegmentedControlif you want a standard UI-Element or configure a UISlider with a range of 2:

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

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




回答2:


Not using the built-in UISwitch. You'll need to roll your own.




回答3:


Why not use a UISegmentedControl?




回答4:


Using UISlider is a good approach. But you would additionally want to adjust the mechanics of your UISlider to be more UISwitch-like. I.e., when you change its position incompletely then it should bounce back to the home position.

Here's what I ended up doing (using a part of FelixLam's 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];

Along with...

- (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];
}


来源:https://stackoverflow.com/questions/2147462/triple-uiswitch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!