Clean up brush stroke gesture usage.

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

@ -21,8 +21,6 @@ public class ImageEditorBrushViewController: OWSViewController {
private let paletteView: ImageEditorPaletteView
private var brushGestureRecognizer: ImageEditorPanGestureRecognizer?
// We only want to let users undo changes made in this view.
// So we snapshot any older "operation id" and prevent
// users from undoing it.
@ -68,8 +66,8 @@ public class ImageEditorBrushViewController: OWSViewController {
let brushGestureRecognizer = ImageEditorPanGestureRecognizer(target: self, action: #selector(handleBrushGesture(_:)))
brushGestureRecognizer.maximumNumberOfTouches = 1
brushGestureRecognizer.referenceView = canvasView.gestureReferenceView
brushGestureRecognizer.delegate = self
self.view.addGestureRecognizer(brushGestureRecognizer)
self.brushGestureRecognizer = brushGestureRecognizer
updateNavigationBar()
}
@ -171,7 +169,7 @@ public class ImageEditorBrushViewController: OWSViewController {
// Apply the location history of the gesture so that the stroke reflects
// the touch's movement before the gesture recognized.
for location in gestureRecognizer.locations {
for location in gestureRecognizer.locationHistory {
tryToAppendStrokeSample(location)
}
@ -230,3 +228,13 @@ extension ImageEditorBrushViewController: ImageEditorPaletteViewDelegate {
// 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("")
guard let locationStart = gestureRecognizer.locationStart else {
guard let locationStart = gestureRecognizer.locationFirst else {
owsFailDebug("Missing locationStart.")
return
}
@ -666,7 +666,7 @@ class ImageEditorCropViewController: OWSViewController {
owsFailDebug("Missing pinchTransform.")
return
}
guard let oldLocationView = gestureRecognizer.locationStart else {
guard let oldLocationView = gestureRecognizer.locationFirst else {
owsFailDebug("Missing locationStart.")
return
}
@ -685,7 +685,7 @@ class ImageEditorCropViewController: OWSViewController {
}
private func cropRegion(forGestureRecognizer gestureRecognizer: ImageEditorPanGestureRecognizer) -> CropRegion? {
guard let location = gestureRecognizer.locationStart else {
guard let location = gestureRecognizer.locationFirst else {
owsFailDebug("Missing locationStart.")
return nil
}

@ -14,39 +14,57 @@ public class ImageEditorPanGestureRecognizer: UIPanGestureRecognizer {
public weak var referenceView: UIView?
// Capture the location history of this gesture.
public var locations = [CGPoint]()
public var locationHistory = [CGPoint]()
public var locationStart: CGPoint? {
return locations.first
public var locationFirst: CGPoint? {
return locationHistory.first
}
// MARK: - Touch Handling
@objc
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
updateLocationHistory(event: event)
guard let referenceView = referenceView else {
owsFailDebug("Missing view")
return
}
locations.append(location(in: referenceView))
super.touchesBegan(touches, with: event)
}
@objc
public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
updateLocationHistory(event: 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 {
owsFailDebug("Missing view")
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() {
super.reset()
locations.removeAll()
locationHistory.removeAll()
}
}

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

Loading…
Cancel
Save