Respond to CR.

pull/2/head
Matthew Chen 7 years ago
parent de27ed8728
commit d419709ebd

@ -56,7 +56,7 @@ public class ImageEditorPaletteView: UIView {
guard let strongSelf = self else { guard let strongSelf = self else {
return return
} }
strongSelf.updateState(fireEvent: false) strongSelf.updateState()
} }
imageView.addSubview(selectionWrapper) imageView.addSubview(selectionWrapper)
selectionWrapper.autoPinEdgesToSuperviewEdges() selectionWrapper.autoPinEdgesToSuperviewEdges()
@ -67,10 +67,17 @@ public class ImageEditorPaletteView: UIView {
selectionWrapper.addSubview(selectionView) selectionWrapper.addSubview(selectionView)
selectionView.autoHCenterInSuperview() selectionView.autoHCenterInSuperview()
// There must be a better way to pin the selection view's location,
// but I can't find it.
let selectionConstraint = NSLayoutConstraint(item: selectionView,
attribute: .centerY, relatedBy: .equal, toItem: selectionWrapper, attribute: .top, multiplier: 1, constant: 0)
selectionConstraint.autoInstall()
self.selectionConstraint = selectionConstraint
isUserInteractionEnabled = true isUserInteractionEnabled = true
addGestureRecognizer(PaletteGestureRecognizer(target: self, action: #selector(didTouch))) addGestureRecognizer(PaletteGestureRecognizer(target: self, action: #selector(didTouch)))
updateState(fireEvent: false) updateState()
} }
// 0 = the color at the top of the image is selected. // 0 = the color at the top of the image is selected.
@ -81,10 +88,12 @@ public class ImageEditorPaletteView: UIView {
private func selectColor(atLocationY y: CGFloat) { private func selectColor(atLocationY y: CGFloat) {
selectionAlpha = y.inverseLerp(0, imageView.height(), shouldClamp: true) selectionAlpha = y.inverseLerp(0, imageView.height(), shouldClamp: true)
updateState(fireEvent: true) updateState()
delegate?.selectedColorDidChange()
} }
private func updateState(fireEvent: Bool) { private func updateState() {
var selectedColor = UIColor.white var selectedColor = UIColor.white
if let image = imageView.image, if let image = imageView.image,
let cgImage = image.cgImage { let cgImage = image.cgImage {
@ -100,18 +109,12 @@ public class ImageEditorPaletteView: UIView {
selectionView.backgroundColor = selectedColor selectionView.backgroundColor = selectedColor
// There must be a better way to pin the selection view's location, guard let selectionConstraint = selectionConstraint else {
// but I can't find it. owsFailDebug("Missing selectionConstraint.")
self.selectionConstraint?.autoRemove() return
let selectionY = selectionWrapper.height() * selectionAlpha
let selectionConstraint = NSLayoutConstraint(item: selectionView,
attribute: .centerY, relatedBy: .equal, toItem: selectionWrapper, attribute: .top, multiplier: 1, constant: selectionY)
selectionConstraint.autoInstall()
self.selectionConstraint = selectionConstraint
if fireEvent {
self.delegate?.selectedColorDidChange()
} }
let selectionY = selectionWrapper.height() * selectionAlpha
selectionConstraint.constant = selectionY
} }
// MARK: Events // MARK: Events

Loading…
Cancel
Save