问题
I want to create a custom UISwtich with three positions. Is it possible?
回答1:
You should be using UISegmentedControl
if 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