Update brightness slider to have a trackColor property to update the control

This commit is contained in:
Jonathan Cardasis 2019-04-14 14:27:46 -04:00
parent 777dcbaad3
commit 44566b0c96
2 changed files with 21 additions and 14 deletions

View File

@ -23,6 +23,8 @@ class ViewController: UIViewController {
temp.frame = CGRect(x: 30, y: 70, width: 320, height: 32)
view.addSubview(temp)
temp.trackColor = UIColor.red
colorPicker.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(colorPicker)

View File

@ -15,6 +15,11 @@ public class BrightnessSlider: UIControl, ChromaControlStylable {
didSet { updateControl(to: currentValue) }
}
/// The base color the slider on the track.
public var trackColor: UIColor = .white {
didSet { updateTrackColor(to: trackColor) }
}
/// The value of the color the handle is currently displaying.
public var currentColor: UIColor {
return sliderHandleView.handleColor
@ -54,12 +59,6 @@ public class BrightnessSlider: UIControl, ChromaControlStylable {
updateShadowIfNeeded()
}
/// Sets the tracking color of the slider and updates the gradient view.
public func updateTrackingColor(to color: UIColor, value: CGFloat) {
updateTrackViewGradient(for: color)
currentValue = value
}
// MARK: - Control
public override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
@ -99,7 +98,7 @@ public class BrightnessSlider: UIControl, ChromaControlStylable {
backgroundColor = .clear
setupSliderTrackView()
setupSliderHandleView()
updateControl(to: currentValue)
updateTrackColor(to: trackColor)
}
internal func setupSliderTrackView() {
@ -130,15 +129,11 @@ public class BrightnessSlider: UIControl, ChromaControlStylable {
}
}
// internal func updateControl(to color: UIColor) {
// updateTrackViewGradient(for: color)
// }
internal func updateControl(to value: CGFloat) {
let brightness = 1 - max(0, min(1, value))
var hue: CGFloat = 0
var saturation: CGFloat = 0
currentColor.getHue(&hue, saturation: &saturation, brightness: nil, alpha: nil)
trackColor.getHue(&hue, saturation: &saturation, brightness: nil, alpha: nil)
let newColor = UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0)
@ -150,6 +145,18 @@ public class BrightnessSlider: UIControl, ChromaControlStylable {
moveHandle(to: value)
}
internal func updateTrackColor(to color: UIColor) {
var hue: CGFloat = 0
var saturation: CGFloat = 0
var brightness: CGFloat = 0
color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: nil)
let colorWithMaxBrightness = UIColor(hue: hue, saturation: saturation, brightness: 1, alpha: 1)
updateTrackViewGradient(for: colorWithMaxBrightness)
currentValue = 1 - brightness
}
internal func updateTrackViewGradient(for color: UIColor) {
sliderTrackView.gradientValues = (color, .black)
}
@ -161,6 +168,4 @@ public class BrightnessSlider: UIControl, ChromaControlStylable {
sliderHandleView.frame = CGRect(origin: CGPoint(x: xPos - (size.width / 2), y: 0), size: size)
}
}