我可以使用我获得的值(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)")
}
答案 0 :(得分:4)
请尝试以下代码:
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()
}
}
答案 1 :(得分:0)
试试这个:
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()
}
}
}