其他分享
首页 > 其他分享> > UISlider基本使用

UISlider基本使用

作者:互联网

UISlider是一个很常用的UI控件,调节屏幕亮度或者调节音量大小等很多地方都可以用到,而且使用方便,下面我来介绍一下UISlider的基本使用。

首先介绍一下基本属性和常用方法:

//设置当前slider的值,默认为0.0
@property(nonatomic) float value;
//设置slider的最小值
@property(nonatomic) float minimumValue;
//设置slider的最大值
@property(nonatomic) float maximumValue;  
//设置滑块左边的图片
@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;

 

下面来看一个完整示例,通过滑动条来调整图片的透明度

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)UISlider *slider;
@property(nonatomic,strong)UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.imageView];
    [self.view addSubview:self.slider];
}

//懒加载
- (UISlider *)slider{
    if (_slider == nil) {
        self.slider = [[UISlider alloc]initWithFrame:CGRectMake(20, 60, self.view.frame.size.width - 40, 20)];
        //设置当前slider的值,默认是0
        _slider.value = 0.5;
        //已完成线条颜色
        _slider.minimumTrackTintColor = [UIColor greenColor];
        //未完成部分线条颜色
        _slider.maximumTrackTintColor = [UIColor blackColor];
        //滑块颜色
        _slider.thumbTintColor = [UIColor grayColor];
        //添加事件
        [_slider addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];
        
    }
    return _slider;
}

//懒加载
- (UIImageView *)imageView{
    if (_imageView == nil) {
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 500)];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        _imageView.image = [UIImage imageNamed:@"1"];
        _imageView.layer.masksToBounds = YES;
        _imageView.layer.borderWidth = 1;
        _imageView.alpha = 0.5;
    }
    
    return _imageView;
}

- (void)valueChanged{
    //滑动中随时改变透明度
    [self.imageView setAlpha:self.slider.value];
}


@end

 

标签:基本,nonatomic,nullable,self,slider,使用,imageView,property,UISlider
来源: https://www.cnblogs.com/li-wei203/p/10803144.html