1、分段选择控件 使用多个按钮的时候 可以选择使用 分段选择控件
2、开关按钮
3、滑杆
这几个都是UIControl的子类 包括之前学习的按钮
1、分段选择控件(分段选择控件默认有一个下标。从0开始,可以通过下表来判断按下的是哪个按钮)
分段选择控件需要在初始化的时候给他一个标题数组
初始化
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"娱乐",@"军事",@"科技"]];
segment.frame = CGRectMake(100, 100, 100, 30);
记忆 是否记忆上一个按钮(默认是NO 记忆的 设置成yes 就不再记忆了)后台记忆
segment.momentary = YES;
给扥段按钮添加方法
[segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
实现方法(按下按钮更换背景 颜色)
- (void)segmentAction:(UISegmentedControl *)sender
{
NSLog(@"%ld",sender.selectedSegmentIndex);
switch (sender.selectedSegmentIndex) {
case 0:
bgView.backgroundColor = [UIColor whiteColor];
break;
case 1:
bgView.backgroundColor = [UIColor grayColor];
break;
case 2:
bgView.backgroundColor = [UIColor orangeColor];
break;
default:
break;
}
}
2、开关按钮
开关按钮 一般需要 记录 用户设置的状态
1)、可以后台提供的借口 设置开关按钮的 开关 (可以在不同的设备间同步(信息))
2)、在本地保存设置(只能在这台是设备中使用)
初始化
UISwitch *switchButton = [[UISwitch alloc]initWithFrame:CGRectMake(100, 200, 50, 40)];
添加方法
[switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
把开关按钮的状态存在本地
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
设置wsitch的默认状态(把存在本地的状态取出来 赋给开关)
switchButton.on = [userDefaults boolForKey:@"isOn"];
设置开关按钮 打开时候的 轨道的 颜色
switchButton.onTintColor = [UIColor redColor];
设置开关按钮 关闭时候 轨道颜色
switchButton.tintColor = [UIColor grayColor];
设置开关按钮 小圆圈的颜色
switchButton.thumbTintColor = [UIColor blackColor];
3、滑杆
初始化
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(200, 200, 100, 30)];
设置滑杆的最小值
slider.minimumValue = 1;
设置滑杆的最大值
slider.maximumValue = 10;
设置滑杆的最小值轨道颜色
slider.minimumTrackTintColor = [UIColor redColor];
设置滑杆的最大值轨道颜色
slider.maximumTrackTintColor = [UIColor yellowColor];
设置滑杆默认的位置
slider.value = 1;
设置小圆圈的颜色
slider.thumbTintColor = [UIColor grayColor];
手指触摸到屏幕上的时候出发的方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
手指在屏幕上移动 出发的方法
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
}
手指 离开屏幕时候的状态 (触摸结束)触发
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}