1、案例介紹:兩個開關保持一致;點擊分段控制兩個開關顯示和隱藏;顯示滑塊的值;如圖01ide
圖01圖02圖03atom
2、案例步驟:spa
一、選擇Single View Aplication新建項目,取名cq.29.開關_滑塊_分段控件,如圖02。code
二、Main.storyboard如圖03。orm
三、CQ29ViewController.hblog
》定義leftSwitch、rightSwitch、sliderValue,分別連線it
#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
四、CQ29ViewController.mio
#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; } }