iOS开发学习之路【UI界面】——UISegmentedControl、UISlider、UISwitch
UISegmentedControl 简介
UISegmentedControl 是一个分栏。
代码实现
self.sc = [[UISegmentedControl alloc]initWithItems:@[@"帅哥",@"美女",@"豪车"]];
self.sc.frame = CGRectMake(100, 200, 200, 50);
[self.view addSubview:self.sc];
[self.sc addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
UISlider 简介
UISlider 控件可以从一个连续的区间中选择一个值,例如:当前音量,亮度等。
IB 实现

代码实现
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UISwitch *mySwitch2;
@end
@implementation ViewController
- (IBAction)sliderChange2:(UISlider *)sender {
// NSLog(@"slider");
float value = sender.value;
NSLog(@"value=%f", value);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mySlider2 = [[UISlider alloc]initWithFrame:CGRectMake(20, 300, 320, 10)];
[self.view addSubview:self.mySlider2];
self.mySlider2.minimumValue = 50.0f;
self.mySlider2.maximumValue = 100.0f;
self.mySlider2.tintColor = [UIColor redColor];
[self.mySlider2 addTarget:self action:@selector(sliderChange2:) forControlEvents:UIControlEventValueChanged];
}
@end
UISwitch 简介
UISwitch 是一个开关控件,常用来控制某个功能的开关状态,例如:蓝牙、GPS、WiFi 等。
IB 实现
代码实现
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UISwitch *mySwitch2;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect frame = CGRectMake(30, 30, 100, 30);
self.mySwitch2 = [[UISwitch alloc]initWithFrame:frame];
[self.view addSubview:self.mySwitch2];
[self.mySwitch2 addTarget:self action:@selector(mySwitch:) forControlEvents:UIControlEventValueChanged];
}