UI第七节——UISlider详解

时间:2023-03-08 17:32:28
UI第七节——UISlider详解
- (void)viewDidLoad {
[super viewDidLoad]; // 实例化UISlider,高度对外观没有影响
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(40, 100, 295, 30)]; // 设置Slider的最大值和最小值
slider.maximumValue = 1000;
slider.minimumValue = 900; // 设置Slider的值,thumb会跳到对应的位置
slider.value = 999; #if 0
// 设置颜色
slider.thumbTintColor = [UIColor redColor];
slider.maximumTrackTintColor = [UIColor greenColor];
slider.minimumTrackTintColor = [UIColor purpleColor];
#endif // 最大值和最小值的图标
[slider setMaximumValueImage:[UIImage imageNamed:@"sound_max"]];
[slider setMinimumValueImage:[UIImage imageNamed:@"sound_min"]]; // Thumb图标
[slider setThumbImage:[UIImage imageNamed:@"slider_thumb"] forState:UIControlStateNormal]; // 滑条的图片,图片一定要设置拉伸区域
[slider setMaximumTrackImage:[[UIImage imageNamed:@"slider_track_max"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 4, 4)] forState:UIControlStateNormal];
[slider setMinimumTrackImage:[[UIImage imageNamed:@"slider_track_min"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 4, 4)] forState:UIControlStateNormal]; // 添加Slider滑动事件
[slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged]; // 把Slider添加到self.view上
[self.view addSubview:slider];
} - (void)sliderValueChanged:(UISlider *)slider
{
// Slider当前位置的值
NSLog(@"%f", slider.value);
}