ios 在swift中将UISlider轨道颜色更改为多种颜色?

oyxsuwqo  于 2022-11-19  发布在  iOS
关注(0)|答案(3)|浏览(260)

我可以用我得到的值(sender.value)来改变滑块移动时的颜色吗?我正在尝试改变滑块移动时的颜色,从绿色到黄色再到红色。请迅速帮助,谢谢!

let brokeSlider = UISlider(frame:CGRectMake(5, 440, 355, 20))
            brokeSlider.minimumValue = 0
            brokeSlider.maximumValue = 1
            brokeSlider.continuous = true
            brokeSlider.tintColor = UIColor.blackColor()
            brokeSlider.value = 0
            brokeSlider.addTarget(self, action: #selector(AnnotationPhotoWidget.sliderValueDidChange(_:)), forControlEvents: .ValueChanged)
            self.view.addSubview(brokeSlider)

    func sliderValueDidChange(sender:UISlider) {
            print("value--\(sender.value)")
    }
hgtggwj0

hgtggwj01#

请尝试以下代码:

func sliderValueDidChange(sender:UISlider) {
     print("value--\(sender.value)")
     if sender.value <= 0.3 {
        brokeSlider.minimumTrackTintColor = UIColor.green
     } else if sender.value > 0.3 && sender.value <= 0.6 {
           brokeSlider.minimumTrackTintColor = UIColor.yellow
     } else {
          brokeSlider.minimumTrackTintColor = UIColor.red
     }
}
jmo0nnb3

jmo0nnb32#

试试看:

import Foundation
import UIKit

class AnnotationPhotoWidget: UIViewController  {
    let brokeSlider = UISlider(frame:CGRectMake(5, 440, 355, 20))

    override func viewDidLoad() {
        brokeSlider.minimumValue = 0
        brokeSlider.maximumValue = 1
        brokeSlider.continuous = true
        brokeSlider.tintColor = UIColor.blackColor()
        brokeSlider.value = 0
        brokeSlider.addTarget(self, action: #selector(AnnotationPhotoWidget.sliderValueDidChange(_:)),forControlEvents: .
            ValueChanged)
        self.view.addSubview(brokeSlider)
    }

    func sliderValueDidChange(sender: UISlider) {
        if sender.value <= 0.3 {
            brokeSlider.minimumTrackTintColor = UIColor.greenColor()
        } else if sender.value > 0.3 && sender.value <= 0.6 {
            brokeSlider.minimumTrackTintColor = UIColor.yellowColor()
        } else {
            brokeSlider.minimumTrackTintColor = UIColor.redColor()
        }
    }
}
w8rqjzmb

w8rqjzmb3#

@ashimVerma的解决方案效果很好。要想让它更上一层楼,获得平滑的色彩过渡,不妨给予这个:

@objc func sliderValueDidChange(sender: UISlider) {
    let saturation = 0.9 // 0...1
    let lightness = 0.46 // 0...1

    let sliderPercentage = sender.value / sender.maximumValue
    let invertedPercentage = 1 - sliderPercentage
    let hue = CGFloat(invertedPercentage * 120 / 360)

    let brightness = lightness + saturation * min(lightness, 1 - lightness)

    var adjustedSaturation: CGFloat = 0.0
    if brightness > 0 {
        adjustedSaturation = 2 * (1 - lightness / brightness)
    }

    let trackColor = UIColor(hue: hue, saturation: adjustedSaturation, brightness: brightness, alpha: 1)
    brokeSlider.minimumTrackTintColor = trackColor
}

这是通过利用HSL颜色空间来实现的,在HSL颜色空间中饱和度和亮度是恒定的。因此,我们只需要根据总色调范围的一部分来改变色调(从绿色到红色)。然后,我们将这些值转换为使用UIColor的HSB初始化器。
参考:https://en.wikipedia.org/wiki/Hue

更简单:

如果不需要在HSL颜色空间中工作,则可以通过直接设置饱和度和亮度值来进一步简化此操作。

@objc func sliderValueDidChange(sender: UISlider) {
    let sliderPercentage = sender.value / sender.maximumValue
    let invertedPercentage = 1 - sliderPercentage
    let hue = CGFloat(invertedPercentage * 120 / 360)

    let trackColor = UIColor(hue: hue, saturation: 0.95, brightness: 0.87, alpha: 1)
    brokeSlider.minimumTrackTintColor = trackColor
}

相关问题