Clean up brush stroke gesture usage.

pull/2/head
Matthew Chen 7 years ago
parent 3d96cd488e
commit 1a159d4d70

@ -21,8 +21,6 @@ public class ImageEditorBrushViewController: OWSViewController {
private let paletteView: ImageEditorPaletteView private let paletteView: ImageEditorPaletteView
private var brushGestureRecognizer: ImageEditorPanGestureRecognizer?
// We only want to let users undo changes made in this view. // We only want to let users undo changes made in this view.
// So we snapshot any older "operation id" and prevent // So we snapshot any older "operation id" and prevent
// users from undoing it. // users from undoing it.
@ -68,8 +66,8 @@ public class ImageEditorBrushViewController: OWSViewController {
let brushGestureRecognizer = ImageEditorPanGestureRecognizer(target: self, action: #selector(handleBrushGesture(_:))) let brushGestureRecognizer = ImageEditorPanGestureRecognizer(target: self, action: #selector(handleBrushGesture(_:)))
brushGestureRecognizer.maximumNumberOfTouches = 1 brushGestureRecognizer.maximumNumberOfTouches = 1
brushGestureRecognizer.referenceView = canvasView.gestureReferenceView brushGestureRecognizer.referenceView = canvasView.gestureReferenceView
brushGestureRecognizer.delegate = self
self.view.addGestureRecognizer(brushGestureRecognizer) self.view.addGestureRecognizer(brushGestureRecognizer)
self.brushGestureRecognizer = brushGestureRecognizer
updateNavigationBar() updateNavigationBar()
} }
@ -171,7 +169,7 @@ public class ImageEditorBrushViewController: OWSViewController {
// Apply the location history of the gesture so that the stroke reflects // Apply the location history of the gesture so that the stroke reflects
// the touch's movement before the gesture recognized. // the touch's movement before the gesture recognized.
for location in gestureRecognizer.locations { for location in gestureRecognizer.locationHistory {
tryToAppendStrokeSample(location) tryToAppendStrokeSample(location)
} }
@ -230,3 +228,13 @@ extension ImageEditorBrushViewController: ImageEditorPaletteViewDelegate {
// TODO: // TODO:
} }
} }
// MARK: -
extension ImageEditorBrushViewController: UIGestureRecognizerDelegate {
@objc public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
// Ignore touches that begin inside the palette.
let location = touch.location(in: paletteView)
return !paletteView.bounds.contains(location)
}
}

@ -501,7 +501,7 @@ class ImageEditorCropViewController: OWSViewController {
Logger.verbose("") Logger.verbose("")
guard let locationStart = gestureRecognizer.locationStart else { guard let locationStart = gestureRecognizer.locationFirst else {
owsFailDebug("Missing locationStart.") owsFailDebug("Missing locationStart.")
return return
} }
@ -666,7 +666,7 @@ class ImageEditorCropViewController: OWSViewController {
owsFailDebug("Missing pinchTransform.") owsFailDebug("Missing pinchTransform.")
return return
} }
guard let oldLocationView = gestureRecognizer.locationStart else { guard let oldLocationView = gestureRecognizer.locationFirst else {
owsFailDebug("Missing locationStart.") owsFailDebug("Missing locationStart.")
return return
} }
@ -685,7 +685,7 @@ class ImageEditorCropViewController: OWSViewController {
} }
private func cropRegion(forGestureRecognizer gestureRecognizer: ImageEditorPanGestureRecognizer) -> CropRegion? { private func cropRegion(forGestureRecognizer gestureRecognizer: ImageEditorPanGestureRecognizer) -> CropRegion? {
guard let location = gestureRecognizer.locationStart else { guard let location = gestureRecognizer.locationFirst else {
owsFailDebug("Missing locationStart.") owsFailDebug("Missing locationStart.")
return nil return nil
} }

@ -14,39 +14,57 @@ public class ImageEditorPanGestureRecognizer: UIPanGestureRecognizer {
public weak var referenceView: UIView? public weak var referenceView: UIView?
// Capture the location history of this gesture. // Capture the location history of this gesture.
public var locations = [CGPoint]() public var locationHistory = [CGPoint]()
public var locationStart: CGPoint? { public var locationFirst: CGPoint? {
return locations.first return locationHistory.first
} }
// MARK: - Touch Handling // MARK: - Touch Handling
@objc @objc
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event) updateLocationHistory(event: event)
guard let referenceView = referenceView else { super.touchesBegan(touches, with: event)
owsFailDebug("Missing view")
return
}
locations.append(location(in: referenceView))
} }
@objc @objc
public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) { public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
updateLocationHistory(event: event)
super.touchesMoved(touches, with: event) super.touchesMoved(touches, with: event)
}
@objc
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
updateLocationHistory(event: event)
super.touchesEnded(touches, with: event)
}
private func updateLocationHistory(event: UIEvent) {
guard let touches = event.allTouches,
touches.count > 0 else {
owsFailDebug("no touches.")
return
}
guard let referenceView = referenceView else { guard let referenceView = referenceView else {
owsFailDebug("Missing view") owsFailDebug("Missing view")
return return
} }
locations.append(location(in: referenceView)) // Find the centroid.
var location = CGPoint.zero
for touch in touches {
location = location.plus(touch.location(in: referenceView))
}
location = location.times(CGFloat(1) / CGFloat(touches.count))
locationHistory.append(location)
} }
public override func reset() { public override func reset() {
super.reset() super.reset()
locations.removeAll() locationHistory.removeAll()
} }
} }

@ -274,7 +274,7 @@ public class ImageEditorView: UIView {
switch gestureRecognizer.state { switch gestureRecognizer.state {
case .began: case .began:
guard let locationStart = gestureRecognizer.locationStart else { guard let locationStart = gestureRecognizer.locationFirst else {
owsFailDebug("Missing locationStart.") owsFailDebug("Missing locationStart.")
return return
} }
@ -294,7 +294,7 @@ public class ImageEditorView: UIView {
guard let textItem = movingTextItem else { guard let textItem = movingTextItem else {
return return
} }
guard let locationStart = gestureRecognizer.locationStart else { guard let locationStart = gestureRecognizer.locationFirst else {
owsFailDebug("Missing locationStart.") owsFailDebug("Missing locationStart.")
return return
} }

Loading…
Cancel
Save