开发者

Triple UISwitch

开发者 https://www.devze.com 2022-12-18 18:42 出处:网络
I want to create a custom UISwtich with three positions. Is开发者_StackOverflow it possible?You should be using UISegmentedControlif you want a standard UI-Element or configure a UISlider with a range

I want to create a custom UISwtich with three positions. Is开发者_StackOverflow it possible?


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.


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


Why not use a UISegmentedControl?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号