zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    SharePoint 2010 不用工作流模拟工作流表单审批的解决方案
    SharePoint 2010 传入电子邮件和传出电子邮件做成收件箱和发件箱(理解可行性)
    SharePoint 2010 准备虚拟机开发环境出现的问题和解决方式
    使用Shell脚本对Linux系统和进程资源进行监控(转)
    你需要知道的16个Linux服务器监控命令(转)
    数字音频采样率与码率(转)
    ffmpeg libx264 编码 "use an encoding preset (e.g. vpre medium)" 错误解决
    一致性哈希算法consistent hashing
    TIME_WAIT 数量增加解决办法
    linux bc命令使用(转)
  • 原文地址:https://www.cnblogs.com/li-wei203/p/10803144.html
Copyright © 2011-2022 走看看