iOS开发之 分段选择控件 开关 滑杆
2015-09-15 16:38
555 查看
1、使用多个按钮的时候 可以选择分段选择控件
分段选择控件在初始化的时候需要给他一个标题的数组,让它知道需要初始化多少个分段按钮
<span style="font-size:18px;color:#666666;"> UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"娱乐",@"军事",@"科技"]]; segment.frame = CGRectMake(100, 100, 150, 35); // 设置是否记忆上一个按钮 segment.momentary = YES; [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:segment];</span><span style="color:#ff2d22;"> </span>
1⃣️、设置是否记忆上一个按钮
segment.momentary = YES;
2⃣️、分段选择控件触发方式
[segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
- (void)segmentAction:(UISegmentedControl *)sender
{
NSLog(@"%ld",sender.selectedSegmentIndex);
switch (sender.selectedSegmentIndex) {
case 0:
bgView.backgroundColor = [UIColor darkGrayColor];
break;
case 1:
bgView.backgroundColor = [UIColor grayColor];
break;
case 2:
bgView.backgroundColor = [UIColor lightGrayColor];
break;
default:
break;
}
}
2、开关按钮
开关按钮一般需要记录用户设置的状态
1⃣️、可以使用后台提供的接口
设置开关按钮的开关(可以在不同设备间同步状态(信息)) 2⃣️、在本地保存设置(只能在本地保存,其他设备上无法保存)
以下为各个属性以及代码的实现
<span style="font-size:14px;"> UISwitch *swithButton = [[UISwitch alloc]initWithFrame:CGRectMake(100, 200, 50, 35)]; [swithButton addTarget:self action:@selector(swithAction:) forControlEvents:UIControlEventValueChanged]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; // 设置switch的默认状态 swithButton.on = [userDefaults boolForKey:@"isOn"]; // 设置开关按钮 打开的时候 轨道的颜色 swithButton.onTintColor = [UIColor redColor]; // 设置开关按钮 关闭时候 轨道的颜色 swithButton.tintColor = [UIColor whiteColor]; // 设置开关按钮 小圆圈的颜色 swithButton.thumbTintColor = [UIColor blueColor]; [self.view addSubview:swithButton];</span>
3、滑杆
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(100, 300, 200, 10)];
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// 设置滑杆的最小值
slider.minimumValue = 0.0;
// 设置滑杆的最大值
slider.maximumValue = 10.0;
// 设置滑杆默认的位置
slider.value = 50.0;
// 设置滑杆最小值的轨道颜色
slider.minimumTrackTintColor = [UIColor greenColor];
// 设置滑杆最大值的轨道颜色
slider.maximumTrackTintColor = [UIColor redColor];
// 设置小圆圈的颜色
slider.thumbTintColor = [UIColor yellowColor];
[self.view addSubview:slider];
- (void)sliderAction:(UISlider *)sender
{
NSLog(@"%0.2f",sender.value);
// self.view.alpha = sender.value/10;
animationView.animationDuration = sender.value;
}
<span style="font-size:14px;">4、// 手指 触摸到屏幕上得时候 触发 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isOn"]!=NO) { // 获得触摸事件 UITouch *touch = [touches anyObject]; // 获得触摸的点 CGPoint touchPoint = [touch locationInView:self.view]; // 动画没有执行的时候 调用这个方法 if (animationView.isAnimating !=YES) { animationView.alpha = 1; animationView.center = touchPoint; [animationView startAnimating]; } } 5、手指在屏幕上移动触发 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // 获得触摸事件 UITouch *touch = [touches anyObject]; // 获得触摸的点 CGPoint touchPoint = [touch locationInView:self.view]; animationView.center = touchPoint; [animationView startAnimating]; } 6、// 手指离开屏幕的时候触发 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [UIView animateWithDuration:2 animations:^{ animationView.alpha = 0.0; } completion:^(BOOL finished) { [animationView stopAnimating]; }]; }</span>
相关文章推荐
- iOS 调取本地相册/相机,剪裁图片进行头像上传
- iOS获取plist文件属性值的方法
- iOS开发之 手势
- iOS 不要使用tag传递TableViewCell的indexPath值
- ios 定位 航向检测
- SVProgressHUD–比MBProgressHUD更好用的 iOS进度提示组件
- SVProgressHUD–比MBProgressHUD更好用的 iOS进度提示组件
- iOS 定位精度
- ios socket第三方框架 AsyncSocket使用简介,连接,心跳,断线,数据发送与接收
- iOS学习资源搜集
- IOS开发+(void)load与+(void)initialize区别
- Crashlytics——iOS crash log 解析利器
- ios 线程安全单例写法
- 【投稿】iOS蓝牙开发(一)蓝牙相关基础知识
- iOS runtime方法详解之添加类
- IOS贝塞尔曲线圆形进度条和加载动画
- ios 关于scrollview内空间位置与想象不符的解决办法
- iOS设置分割线从边框顶端开始
- ios 读取图片
- VMware虚拟机 停电 无法启动