您的位置:首页 > 移动开发 > IOS开发

iOS开发之 分段选择控件 开关 滑杆

2015-09-15 16:38 519 查看


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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: