一、UISlider
UISlider是iOS中的滑块控件
通常用于控制视频播放进度,控制音量
它继承于UIControl,滑块位于滑条不同位置时提供不同的Value值
//滑竿初始化
UISlider *mySlider = [[UISlider alloc]initWithFrame:CGRectMake(22, 22, 300, 50)];
//设置滑竿值范围
mySlider.minimumValue = 0.5;
mySlider.maximumValue = 3;
//设置起始值
mySlider.value = 1;
//设置滑过范围的颜色
mySlider.minimumTrackTintColor = [UIColor grayColor];
//未滑过的颜色
mySlider.maximumTrackTintColor = [UIColor redColor];
//指定左边和右边指示图 图像大小30X30
mySlider.minimumValueImage = [UIImage imageNamed:@"bc.jpg"];
mySlider.maximumValueImage = [UIImage imageNamed:@"bc.jpg"];
//设置滑块的颜色
mySlider.thumbTintColor = [UIColor greenColor];
mySlider.tag = 1004;
//设置滑块的图片
[mySlider setThumbImage: [UIImage imageNamed:@"bc.jpg"]forState:UIControlStateNormal];
//添加触发事件,滑动滑竿触发
[mySlider addTarget:self action:@selector(changeTransform:) forControlEvents:UIControlEventValueChanged];
//添加到父视图
[self.view addSubview:mySlider];
可以练习创建三个UISlider,通过它们改变背景的RGB值,达到调节背景颜色的效果,没啥难度
二、UISegmentControl
UISegmentControl是iOS中常用的分段控件,它相当于集成了多个Button
可以使用分段控件切换不同的视图
//初始化,items 根据数组在的元素创建相应个数的按钮
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"红",@"绿",@"蓝", nil nil]];
//设置大小
segmentedControl.frame = CGRectMake(100, 100, 200, 50);
//指定被选中的分段
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl setSelectedSegmentIndex:1];
//更改分段控制器颜色
[segmentedControl setTintColor:[UIColor grayColor]];
//给每个分段设置图片
//渲染模式imageWithRenderingMode 设置 为UIImageRenderingModeAlwaysOriginal
//否则会根据当前的颜色对图片进行颜色渲染
[segmentedControl setImage:[[UIImage imageNamed:@"bc.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:1];
//添加点击事件
[segmentedControl addTarget:self action:@selector(changeBgColor:) forControlEvents:UIControlEventValueChanged];
//设置样式,已经废弃
// segmentedControl.segmentedControlStyle
[self.view addSubview:segmentedControl];
下面做一个小练习,通过UISegmentControl改变视图的颜色
#pragma mark 三个界面通过分段控制器切换
//创建三个视图
for (int i = 0; i < 3; i ++) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 375, 300)];
view.tag = 1000+i;
//设置背景色为随机
view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
[self.view addSubview:view];
// if (i == 0) {
// view.hidden = NO;
// }
// else
// view.hidden = YES;
}
//创建分段控制器
UISegmentedControl *changeViewSegment = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"1",@"2",@"3", nil nil]];
changeViewSegment.frame = CGRectMake(80, 100, 200, 30);
//默认选中第一个分段,设置tag值
changeViewSegment.selectedSegmentIndex = 0;
changeViewSegment.tag = 1003;
//添加方法,点击分段切换颜色
[changeViewSegment addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:changeViewSegment];
<pre name="code" class="objc">//segment切换视图方法
-(void)changeView:(UISegmentedControl*)sender
{
//根据Tag值得到三个视图
UIView *firstView = [self.view viewWithTag:1000];
UIView *secondView = [self.view viewWithTag:1001];
UIView *thirdView = [self.view viewWithTag:1002];
//通过修改三个视图的hidden属性实现切换
switch (sender.selectedSegmentIndex) {
case 0:
firstView.hidden = NO;
secondView.hidden = YES;
thirdView.hidden = YES;
break;
case 1:
firstView.hidden = YES;
secondView.hidden = NO;
thirdView.hidden = YES;
break;
case 2:
firstView.hidden = YES;
secondView.hidden = YES;
thirdView.hidden = NO;
break;
default:
break;
}
}
实现效果如下: