- UISlider是iOS中的滑块控件
- 通常⽤于控制视频播放进度,控制⾳量等。
- 它继承于UIControl,滑块提供了⼀系列连续的值,滑块停 在不同的位置,获取到滑块上的值也不同。
minimumValue //设置滑块的最⼩值
maximumValue //设置滑块的最⼤值
value //设置滑块的当前值
minimumTrackTinkColor //定义划过区域的颜⾊
addTarget:action:forControlEvents: //给slider添加事件
注意:controlEvent为UIControlEventValueChanged
注意:controlEvent为UIControlEventValueChanged
原因:滑块上的每个点都对应特定范围内的某个值,所以当值变化时触发slider事件
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 150, 150, 100)];
imageView.center = CGPointMake(self.view.center.x, 200);
imageView.image = [UIImage imageNamed:@"2.JPG"];
imageView.tag = 101;
[self.view addSubview:imageView];
[imageView release];
//创建一个UISlider
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(self.view.center.x - 100, imageView.frame.origin.y + imageView.frame.size.height + 20, 200, 20)];
//设置滑块的最小值
slider.minimumValue = 0.0;
//设置滑块的最大值
slider.maximumValue = 225.0;
//设置滑块的当前值
slider.value = 112.5;
//定义划过区域的颜色
slider.minimumTrackTintColor = [UIColor darkGrayColor];
//给slider添加事件
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
imageView.center = CGPointMake(self.view.center.x, 200);
imageView.image = [UIImage imageNamed:@"2.JPG"];
imageView.tag = 101;
[self.view addSubview:imageView];
[imageView release];
//创建一个UISlider
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(self.view.center.x - 100, imageView.frame.origin.y + imageView.frame.size.height + 20, 200, 20)];
//设置滑块的最小值
slider.minimumValue = 0.0;
//设置滑块的最大值
slider.maximumValue = 225.0;
//设置滑块的当前值
slider.value = 112.5;
//定义划过区域的颜色
slider.minimumTrackTintColor = [UIColor darkGrayColor];
//给slider添加事件
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
[slider release];
- (void)sliderAction:(UISlider *)slider {
CGPoint newCenter = CGPointMake(slider.value + 75, 200);
UIImageView *image = (UIImageView *)[self.view viewWithTag:101];
image.center = newCenter;
CGRect newBounds = CGRectMake(0, 0, slider.value + 50, slider.value + 0);
image.animationDuration = slider.value;
image.bounds = newBounds;
NSLog(@"%.1f",slider.value);
CGPoint newCenter = CGPointMake(slider.value + 75, 200);
UIImageView *image = (UIImageView *)[self.view viewWithTag:101];
image.center = newCenter;
CGRect newBounds = CGRectMake(0, 0, slider.value + 50, slider.value + 0);
image.animationDuration = slider.value;
image.bounds = newBounds;
NSLog(@"%.1f",slider.value);
}