UISlider

UISlider

  • 常用属性
1.@property(nonatomic) float value;                                 // default 0.0. this value will be pinned to min/max
2.@property(nonatomic) float minimumValue; // default 0.0. the current value may change if outside new min value
3.@property(nonatomic) float maximumValue; // default 1.0. the current value may change if outside new max value
4.@property(nullable, nonatomic,strong) UIColor *minimumTrackTintColor;
5.@property(nullable, nonatomic,strong) UIColor *maximumTrackTintColor;
6.@property(nullable, nonatomic,strong) UIColor *thumbTintColor
  • 常用方法
1.- (void)setValue:(float)value animated:(BOOL)animated;
2.- (void)setThumbImage:(nullable UIImage *)image forState:(UIControlState)state;
3.- (void)setMinimumTrackImage:(nullable UIImage *)image forState:(UIControlState)state;
4.- (void)setMaximumTrackImage:(nullable UIImage *)image forState:(UIControlState)state;
  • 注意: 设置trackImage时,需要对image作处理,官方文档是这么说的:The track images should be 3 part resizable (via UIImage’s resizableImage methods) along the direction that is longer
  • 示例1
1.
2.- (void)viewDidLoad {
3. [super viewDidLoad];
4. // Do any additional setup after loading the view, typically from a nib.
5.
6. self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.0 blue:1.0 alpha:1];
7.
8.
9. UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
10. slider.center = self.view.center;
11.
12. slider.backgroundColor = [UIColor grayColor];
13.
14. slider.minimumValue = 0;
15.
16. slider.maximumValue = 1;
17.
18. slider.minimumTrackTintColor = [UIColor greenColor];
19.
20. slider.maximumTrackTintColor = [UIColor magentaColor];
21.
22. slider.thumbTintColor = [UIColor cyanColor];
23.
24. [self.view addSubview:slider];
25.
26. [slider addTarget: self action:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];
27.
28.}
29.
30.-(void)changeColor:(UISlider *)sender{
31. self.view.backgroundColor = [UIColor colorWithRed:0.5 green:sender.value blue:1.0 alpha:1];
32.}

Alt text

  • 注:滑块大小不随frame的值改变而改变,如果设置了thumImage那么滑块的大小同image一样。当手触摸滑块的时候,滑块的状态为hightlighted

  • 示例2(设置imgae)

    • 直接使用image
1.     [slider setThumbImage:[UIImage imageNamed:@"SliderThumb-Highlighted"] forState:UIControlStateHighlighted];
2. [slider setThumbImage:[UIImage imageNamed:@"SliderThumb-Normal"] forState:UIControlStateNormal];
3.
4. UIImage *leftImage = [UIImage imageNamed:@"SliderTrackLeft"];
5. UIImage *rightImage = [UIImage imageNamed:@"SliderTrackRight"];
6.
7. [slider setMinimumTrackImage:leftImage forState:UIControlStateNormal];
8. [slider setMaximumTrackImage:rightImage forState:UIControlStateNormal];

Alt text
可以看见,trackImage已经发生了形变,这绝对不是我们想要的效果。

  • 对image进行处理一下
1.    leftImage = [leftImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
2. rightImage = [rightImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];

Alt text

 

你可能感兴趣的:(UISlider)