UISlider基本使用

UISlider是一個很經常使用的UI控件,調節屏幕亮度或者調節音量大小等不少地方均可以用到,並且使用方便,下面我來介紹一下UISlider的基本使用。ide

首先介紹一下基本屬性和經常使用方法:atom

//設置當前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;

 

下面來看一個完整示例,經過滑動條來調整圖片的透明度spa

#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
相關文章
相關標籤/搜索