iOS UISlider

本文详细介绍了UISlider控件的常用属性与方法,并提供了一个控制图片透明度的实例。通过设置UISlider的各种属性,可以定制滑动条的外观样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

在这里插入图片描述
代码:https://github.com/ShaeZhuJiu/UISlider_base.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值