OC之UISlider

SONG / 文 发表于2018-01-29 09:49 次阅读 uislider

一、初始化 // 高度不起作用,但不能为 0 ,为 0 时滑块无法滑动

UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 200, 300, 20)];

二、设置值

    // 设置最小和最大值
    slider.minimumValue = 0;
    slider.maximumValue = 100;

    // 设置当前值
    slider.value = 50;
    [slider setValue:50 animated:YES];

    // 获取当前值
    float value = slider.value;
     // 获取当前滑块的图片值
    UIImage *thumbImage = slider.currentThumbImage;
    // 获取当前滑过的进度条的图片值
    UIImage *minimumTrackImage = slider.currentMinimumTrackImage;
    // 获取当前未滑过的进度条的图片值
    UIImage *maximumTrackImage = slider.currentMaximumTrackImage;

三、设置颜色

    /*
        默认为白色
    */
    slider.thumbTintColor = [UIColor orangeColor];

    /*
        默认为蓝色
    */
    slider.tintColor = [UIColor redColor];

    // 设置滑过的和未划过的进度条的颜色
    /*
        minimumTrackTintColor:滑过的进度条颜色,默认为蓝色
        maximumTrackTintColor:未划过的进度条颜色,默认为灰色
    */
    slider.minimumTrackTintColor = [UIColor greenColor];
    slider.maximumTrackTintColor = [UIColor redColor];

四、设置图片

// 设置滑块的图片
[slider setThumbImage:[UIImage imageNamed:@"block"] forState:UIControlStateNormal];
[slider setThumbImage:[UIImage imageNamed:@"block1"] forState:UIControlStateHighlighted];
// 设置滑过的和未划过的进度条的图片
[slider setMinimumTrackImage:[UIImage imageNamed:@"pic1"] forState:UIControlStateNormal];
[slider setMaximumTrackImage:[UIImage imageNamed:@"pic2"] forState:UIControlStateNormal];
    // 设置左右两端的图片
slider.minimumValueImage = [UIImage imageNamed:@"num-"];
slider.maximumValueImage = [UIImage imageNamed:@"num+"];

五、设置纵向

// 顺时针旋转 90 度,90/180 * M_PI,1 度 = PI/180 弧度
slider.transform = CGAffineTransformMakeRotation(0.5 * CGFloat(M_PI))
// 逆时针旋转 90 度,270/180 * M_PI,1 度 = PI/180 弧度
slider.transform = CGAffineTransformMakeRotation(1.5 * CGFloat(M_PI))

六、设置点击事件

 [slider addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventValueChanged];

七、设置透明

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0f);
    UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [slider setMinimumTrackImage:transparentImage forState:UIControlStateNormal];
    [slider setMaximumTrackImage:transparentImage forState:UIControlStateNormal];
收藏 赞 (0) 踩 (0)