From 1a159d4d70916c668600b051e8e187214918d84d Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 1 Mar 2019 10:37:36 -0500 Subject: [PATCH] Clean up brush stroke gesture usage. --- .../ImageEditorBrushViewController.swift | 16 ++++++-- .../ImageEditorCropViewController.swift | 6 +-- .../ImageEditorPanGestureRecognizer.swift | 40 ++++++++++++++----- .../Views/ImageEditor/ImageEditorView.swift | 4 +- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorBrushViewController.swift b/SignalMessaging/Views/ImageEditor/ImageEditorBrushViewController.swift index 0ba64c3c2..afa220829 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorBrushViewController.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorBrushViewController.swift @@ -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) + } +} diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorCropViewController.swift b/SignalMessaging/Views/ImageEditor/ImageEditorCropViewController.swift index 814a403ac..e3e2b0b0d 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorCropViewController.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorCropViewController.swift @@ -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 } diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorPanGestureRecognizer.swift b/SignalMessaging/Views/ImageEditor/ImageEditorPanGestureRecognizer.swift index 835df3b93..699a2c832 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorPanGestureRecognizer.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorPanGestureRecognizer.swift @@ -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, 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, with event: UIEvent) { + updateLocationHistory(event: event) + super.touchesMoved(touches, with: event) + } + + @objc + public override func touchesEnded(_ touches: Set, 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() } } diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift index ba38915eb..8c31e1907 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift @@ -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 }