转载自:http://blog.csdn.net/weisubao/article/details/39559001
(1)滑动条的左右端背景可以设置上一页下一页的图片;
(2)滑动条的轨道图片可以设置为渐变等等图片。
(3)滑动条因为值可以互动,所以addTarget:方法很重要,其中事件值变动UIControlEventValueChanged比较特殊,其实和按钮的按下事件是一个性质,都是一个事件而已。
- #import "ViewController.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- {
- //应为后面getValue:方法要调用这个对象,所以得弄成全局变量
- UILabel *label1;
- }
- - (void)viewDidLoad {
- //实例化一个滑动条
- UISlider *sli1=[[UISlider alloc]init];
- //设置控件位置和大小,大小不影响控件本身大小,但当高度设置为0,滑块不可拖动
- sli1.frame=CGRectMake(30, 30, 300, 60);
- //设置值
- sli1.value=0.8;
- //设置最小值
- sli1.minimumValue=1;
- //设置最大值
- sli1.maximumValue=10;
- //设置已经滑过一端滑动条颜色
- sli1.minimumTrackTintColor=[UIColor redColor];
- //设置未滑过一端滑动条颜色
- sli1.maximumTrackTintColor=[UIColor blackColor];
- //设置最小值一端图片,会挤压滑动条宽度
- sli1.minimumValueImage=[UIImage imageNamed:@"1.png"];
- //设置最大值一端图片,会挤压滑动条宽度
- sli1.maximumValueImage=[UIImage imageNamed:@"2.png"];
- //设置滑块颜色,貌似无效,可能是默认的时图片,已经覆盖了颜色
- sli1.thumbTintColor=[UIColor yellowColor];
- //设置已经滑过一端滑动条背景图片,会覆盖之前之前的颜色,以下相同
- [sli1 setMinimumTrackImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal];
- //设置未滑过一端滑动条背景图片
- [sli1 setMaximumTrackImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];
- //设置滑块图片背景
- [sli1 setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
- //最重要的就是根据滑动事件来进行相应操作
- //此处和按钮的类似,但是按钮的事件是按下,而这里的事件是UIControlEventValueChanged值变化就是事件
- [sli1 addTarget:self action:@selector(getValue1:) forControlEvents:UIControlEventValueChanged];
- //设置一个标签,把变动的值实时显示出来
- //得在上面弄成全局变量,所以这里就不是UILabel *label1了,而直接初始化即可
- label1=[[UILabel alloc]initWithFrame:CGRectMake(30, 100, 300, 50)];
- label1.text=@"值";
- [self.view addSubview:sli1];
- [self.view addSubview:label1];
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- -(void)getValue1:(id)sender{
- UISlider *sli2=(UISlider *)sender;
- label1.text=[NSString stringWithFormat:@"%f",sli2.value];
- }
- @end
我的代码
-(void) viewDidLoad
{
UISlider* sli1 = [[UISlider alloc] init];
sli1.frame = CGRectMake(30, 30, 300, 60);
sli1.value = 0.8;
sli1.minimumValue = 1;
sli1.maximumValue = 10;
sli1.minimumTrackTintColor = [UIColor redColor];
sli1.maximumTrackTintColor = [UIColor blackColor];
sli1.minimumValueImage = [UIImage imageNamed:@"propIcon_50000001.png"];
sli1.maximumValueImage = [UIImage imageNamed:@"propIcon_50000002.png"];
sli1.thumbTintColor = [UIColor yellowColor];
[sli1 setMinimumTrackImage:[UIImage imageNamed:@"loadingbar.png"] forState:UIControlStateNormal];
[sli1 setMaximumTrackImage:[UIImage imageNamed:@"sliderProgress.png"] forState:UIControlStateNormal];
[sli1 setThumbImage:[UIImage imageNamed:@"propIcon_50000008.png"] forState:UIControlStateNormal];
[sli1 addTarget:self action:@selector(getValue1:) forControlEvents:UIControlEventValueChanged];
label1 = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 300, 50)];
label1.text = @"值";
[self.view addSubview:sli1];
[self.view addSubview:label1];
[super viewDidLoad];
}
-(void) getValue1:(id)sender
{
UISlider* sli2 = (UISlider*) sender;
label1.text = [NSString stringWithFormat:@"%f",sli2.value];
}