Created
December 3, 2019 17:00
-
-
Save digitallysavvy/a0a4b67bc60675cac6103eca8993711d to your computer and use it in GitHub Desktop.
Snippet for handlePan within the ARSupportAudienceViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) { | |
// TODO: send touch started event | |
// keep track of points captured during pan gesture | |
if self.sessionIsActive && (gestureRecognizer.state == .began || gestureRecognizer.state == .changed) { | |
let translation = gestureRecognizer.translation(in: self.view) | |
// calculate touch movement relative to the superview | |
guard let touchStart = self.touchStart else { return } // ignore accidental finger drags | |
let pixelTranslation = CGPoint(x: touchStart.x + translation.x, y: touchStart.y + translation.y) | |
// normalize the touch point to use view center as the reference point | |
let translationFromCenter = CGPoint(x: pixelTranslation.x - (0.5 * self.view.frame.width), y: pixelTranslation.y - (0.5 * self.view.frame.height)) | |
self.touchPoints.append(pixelTranslation) | |
// TODO: Send captured points | |
DispatchQueue.main.async { | |
// draw user touches to the DrawView | |
guard let drawView = self.drawingView else { return } | |
guard let lineColor: UIColor = self.lineColor else { return } | |
let layer = CAShapeLayer() | |
layer.path = UIBezierPath(roundedRect: CGRect(x: pixelTranslation.x, y: pixelTranslation.y, width: 25, height: 25), cornerRadius: 50).cgPath | |
layer.fillColor = lineColor.cgColor | |
drawView.layer.addSublayer(layer) | |
} | |
if debug { | |
print(translationFromCenter) | |
print(pixelTranslation) | |
} | |
} | |
if gestureRecognizer.state == .ended { | |
// TODO: send message to remote user that touches have ended | |
// clear list of points | |
if let touchPointsList = self.touchPoints { | |
self.touchStart = nil // clear starting point | |
if debug { | |
print(touchPointsList) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment