简体   繁体   English

更改 swift 中多个 colors 的 UISlider 轨道颜色?

[英]Change UISlider track color for multiple colors in swift?

Can I use the value I obtain(sender.value) to change the color as the slider moves?当 slider 移动时,我可以使用我获得的值 (sender.value) 来改变颜色吗? I am trying to change the color from green to yellow to red as the slider moves.随着 slider 的移动,我正在尝试将颜色从绿色变为黄色再变为红色。 Please help in swift thanks!请帮忙swift谢谢!

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)")
    }

Try the following code: 尝试以下代码:

func sliderValueDidChange(sender:UISlider) {
     print("value--\(sender.value)")
     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()
     }
}

@ashimVerma's solution works great. @ashimVerma 的解决方案效果很好。 To take it up a notch and get smooth color transitions, give this a whirl:要提高一个档次并获得平滑的颜色过渡,请试一试:

在此处输入图像描述

@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
}

This works by utilizing the HSL color space where saturation and lightness are constant.这是通过利用饱和度和亮度恒定的 HSL 色彩空间来实现的。 So we only need to vary the hue (from green to red) based on a portion of the total hue range.所以我们只需要根据总色调范围的一部分来改变色调(从绿色到红色)。 We then convert those values to use UIColor's HSB initializer.然后我们将这些值转换为使用 UIColor 的 HSB 初始值设定项。

Reference: https://en.wikipedia.org/wiki/Hue参考: https://en.wikipedia.org/wiki/Hue

Even Simpler:更简单:

If you don't need to work in the HSL color space, this can be further simplified by directly setting the saturation and brightness values.如果您不需要在 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
}

Try this: 尝试这个:

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()
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM