Ensure brush strokes include the entire gesture.

pull/2/head
Matthew Chen 7 years ago
parent 65ee1dbd75
commit 919e886eb7

@ -127,7 +127,7 @@ public class ImageEditorBrushViewController: OWSViewController {
private var currentStrokeSamples = [ImageEditorStrokeItem.StrokeSample]() private var currentStrokeSamples = [ImageEditorStrokeItem.StrokeSample]()
@objc @objc
public func handleBrushGesture(_ gestureRecognizer: UIGestureRecognizer) { public func handleBrushGesture(_ gestureRecognizer: ImageEditorPanGestureRecognizer) {
AssertIsOnMainThread() AssertIsOnMainThread()
let removeCurrentStroke = { let removeCurrentStroke = {
@ -137,10 +137,9 @@ public class ImageEditorBrushViewController: OWSViewController {
self.currentStroke = nil self.currentStroke = nil
self.currentStrokeSamples.removeAll() self.currentStrokeSamples.removeAll()
} }
let tryToAppendStrokeSample = { let tryToAppendStrokeSample = { (locationInView: CGPoint) in
let view = self.canvasView.gestureReferenceView let view = self.canvasView.gestureReferenceView
let viewBounds = view.bounds let viewBounds = view.bounds
let locationInView = gestureRecognizer.location(in: view)
let newSample = ImageEditorCanvasView.locationImageUnit(forLocationInView: locationInView, let newSample = ImageEditorCanvasView.locationImageUnit(forLocationInView: locationInView,
viewBounds: viewBounds, viewBounds: viewBounds,
model: self.model, model: self.model,
@ -162,14 +161,22 @@ public class ImageEditorBrushViewController: OWSViewController {
case .began: case .began:
removeCurrentStroke() removeCurrentStroke()
tryToAppendStrokeSample() // 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 {
tryToAppendStrokeSample(location)
}
let locationInView = gestureRecognizer.location(in: canvasView.gestureReferenceView)
tryToAppendStrokeSample(locationInView)
let stroke = ImageEditorStrokeItem(color: strokeColor, unitSamples: currentStrokeSamples, unitStrokeWidth: unitStrokeWidth) let stroke = ImageEditorStrokeItem(color: strokeColor, unitSamples: currentStrokeSamples, unitStrokeWidth: unitStrokeWidth)
model.append(item: stroke) model.append(item: stroke)
currentStroke = stroke currentStroke = stroke
case .changed, .ended: case .changed, .ended:
tryToAppendStrokeSample() let locationInView = gestureRecognizer.location(in: canvasView.gestureReferenceView)
tryToAppendStrokeSample(locationInView)
guard let lastStroke = self.currentStroke else { guard let lastStroke = self.currentStroke else {
owsFailDebug("Missing last stroke.") owsFailDebug("Missing last stroke.")

@ -13,7 +13,12 @@ public class ImageEditorPanGestureRecognizer: UIPanGestureRecognizer {
public weak var referenceView: UIView? public weak var referenceView: UIView?
public var locationStart: CGPoint? // Capture the location history of this gesture.
public var locations = [CGPoint]()
public var locationStart: CGPoint? {
return locations.first
}
// MARK: - Touch Handling // MARK: - Touch Handling
@ -25,12 +30,23 @@ public class ImageEditorPanGestureRecognizer: UIPanGestureRecognizer {
owsFailDebug("Missing view") owsFailDebug("Missing view")
return return
} }
locationStart = self.location(in: referenceView) locations.append(location(in: referenceView))
}
@objc
public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
guard let referenceView = referenceView else {
owsFailDebug("Missing view")
return
}
locations.append(location(in: referenceView))
} }
public override func reset() { public override func reset() {
super.reset() super.reset()
locationStart = nil locations.removeAll()
} }
} }

Loading…
Cancel
Save