Skip to content

Commit

Permalink
Fix classic color picker initial position
Browse files Browse the repository at this point in the history
  • Loading branch information
noppefoxwolf committed Jun 29, 2022
1 parent 3fc8c34 commit a553530
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class ClassicColorPicker: UIControl, ColorPicker {
}
colorView.addSubview(markerView)
markerView.snp.makeConstraints { make in
make.center.equalTo(CGPoint.zero)
make.centerX.equalTo(colorView.snp.right).multipliedBy(1.0)
make.centerY.equalTo(colorView.snp.bottom).multipliedBy(1.0)
make.size.equalTo(36)
}

hueSlider.addAction(UIAction { [unowned self] _ in
Expand Down Expand Up @@ -81,9 +83,13 @@ class ClassicColorPicker: UIControl, ColorPicker {

override func setNeedsUpdateConstraints() {
super.setNeedsUpdateConstraints()
let location = colorView.location(by: color)!
markerView.snp.updateConstraints { make in
make.center.equalTo(location)
let multiply = colorView.locationMultiply(by: color)
let multiplyX = max(multiply.width, .leastNonzeroMagnitude)
let multiplyY = max(multiply.height, .leastNonzeroMagnitude)
markerView.snp.remakeConstraints { make in
make.centerX.equalTo(colorView.snp.right).multipliedBy(multiplyX)
make.centerY.equalTo(colorView.snp.bottom).multipliedBy(multiplyY)
make.size.equalTo(36)
}
}
}
Expand All @@ -104,9 +110,6 @@ class ClassicColorMarkerView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
self.snp.makeConstraints { make in
make.size.equalTo(36)
}
}

required init?(coder: NSCoder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class ClassicColorView: UIView {
return CGPoint(x: hsb.s * bounds.width, y: (1.0 - hsb.v) * bounds.height)
}

func locationMultiply(by color: CGColor) -> CGSize {
let hsb = color.hsb
return CGSize(width: hsb.s, height: (1.0 - hsb.v))
}

override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()!
let maskPath = UIBezierPath(roundedRect: rect, cornerRadius: 8).cgPath
Expand Down

0 comments on commit a553530

Please sign in to comment.