UISlider控件就是我们通常用于调节亮度,透明度,音量时会出现的滑动条。UISlider控件是通过滑块所处的位置来标识数值,它允许用户拖动滑块来改变当前值。

UISlider常用属性与方法
UISlider类中提供了用于设置滑动条的样式属性与方法,常用的有如下几个,通过设置这些属性或者调用相关方法,我们可以灵活的定制滑动条的外观样式。
设置当前slider的值,默认是0。
@property(nonatomic) float value;
设置滑块左边的图片。
@property(nullable, nonatomic,strong) UIImage *minimumValueImage;
设置滑块右边的图片。
@property(nullable, nonatomic,strong) UIImage *maximumValueImage;
滑动条完成部分的轨道颜色。
@property(nullable, nonatomic,strong) UIColor *minimumTrackTintColor;
滑动条未完成部分的轨道颜色。
@property(nullable, nonatomic,strong) UIColor *maximumTrackTintColor;
滑块的颜色。
@property(nullable, nonatomic,strong) UIColor *thumbTintColor;
设置slider的值
-(void)setValue:(float)value animated:(BOOL)animated;
设置不同状态下滑块的图像
-(void)setThumbImage:(nullable UIImage *)image forState:(UIControlState)state;
设置不同状态下滑动条左侧的图像
-(void)setMinimumTrackImage:(nullable UIImage *)image forState:(UIControlState)state;
设置不同状态下滑动条右侧的图像
-(void)setMaximumTrackImage:(nullable UIImage *)image forState:(UIControlState)state;
使用:控制图片透明度
//
// ViewController.m
// UISlider_base
//
// Created by 谢鑫 on 2019/7/17.
// Copyright © 2019 Shae. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UISlider *slider;
@property(nonatomic,strong)UIImageView *imageView;
@end
@implementation ViewController
- (UISlider *)slider{
if (_slider==nil) {
_slider=[[UISlider alloc]initWithFrame:CGRectMake(20, 400, [UIScreen mainScreen].bounds.size.width-40, 20)];
_slider.value=0.5;
_slider.minimumTrackTintColor=[UIColor greenColor];
_slider.maximumTrackTintColor=[UIColor blackColor];
_slider.thumbTintColor=[UIColor grayColor];
[_slider addTarget:self action:@selector(change) forControlEvents:UIControlEventValueChanged];
}
return _slider;
}
- (UIImageView *)imageView{
if (_imageView==nil) {
_imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 220, 220)];
_imageView.image=[UIImage imageNamed:@"1"];
_imageView.layer.masksToBounds=YES;
_imageView.layer.borderWidth=1;
_imageView.alpha=0.5;
}
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.slider];
[self.view addSubview:self.imageView];
}
-(void)change{
self.imageView.alpha=self.slider.value;
}
@end