iOS.UIKit.04.UISwitch_UISegmentedControl
一、案例介绍:两个开关保持一致;点击分段控制两个开关显示和隐藏;显示滑块的值;如图01
图01图02图03
二、案例步骤:
1、选择Single View Aplication新建项目,取名cq.29.开关_滑块_分段控件,如图02。
2、Main.storyboard如图03。
3、CQ29ViewController.h
》定义leftSwitch、rightSwitch、sliderValue,分别连线
#import <UIKit/UIKit.h> @interface CQ29ViewController : UIViewController @property (weak,nonatomic) IBOutlet UISwitch *leftSwitch; @property (weak,nonatomic) IBOutlet UISwitch *rightSwitch; @property (weak,nonatomic) IBOutlet UILabel *sliderValue; - (IBAction)sliderValueChanged:(id)sender; - (IBAction)valueChanged:(id)sender; -(IBAction)touchDown:(id)sender; @end
4、CQ29ViewController.m
#pragma mark 保持两个开关同开同关 - (IBAction)valueChanged:(id)sender { UISwitch *witchSwitch = (UISwitch *)sender; BOOL setting = witchSwitch.isOn; [self.leftSwitch setOn:setting animated:YES]; [self.rightSwitch setOn:setting animated:YES]; } #pragma mark 显示滑块的值 - (IBAction)sliderValueChanged:(id)sender { UISlider *slider = (UISlider *)sender; int progressAsInt = (int)(slider.value + 0.5f); NSString *newText = [[NSString alloc] initWithFormat:@"%d",progressAsInt]; self.sliderValue.text = newText; } //点击分段控件控制开关控件的隐藏或显示 - (IBAction)touchDown:(id)sender { if (self.leftSwitch.hidden == YES) { self.rightSwitch.hidden = NO; self.leftSwitch.hidden = NO; }else{ self.leftSwitch.hidden = YES; self.rightSwitch.hidden = YES; } }