Skip to content

Instantly share code, notes, and snippets.

@digitallysavvy
Created December 3, 2019 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitallysavvy/d28fe8fcd82551cfc7f4010766cc8736 to your computer and use it in GitHub Desktop.
Save digitallysavvy/d28fe8fcd82551cfc7f4010766cc8736 to your computer and use it in GitHub Desktop.
Snippet for handlePan within the ARSupportAudienceViewController (includes data stream commands)
@IBAction func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if self.sessionIsActive && gestureRecognizer.state == .began && self.streamIsEnabled == 0 {
// send message to remote user that touches have started
self.agoraKit.sendStreamMessage(self.dataStreamId, data: "touch-start".data(using: String.Encoding.ascii)!)
}
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)
if self.streamIsEnabled == 0 {
// send data to remote user
let pointToSend = CGPoint(x: translationFromCenter.x, y: translationFromCenter.y)
self.dataPointsArray.append(pointToSend)
if self.dataPointsArray.count == 10 {
sendTouchPoints() // send touch data to remote user
clearSubLayers() // remove touches drawn to the screen
}
if debug {
print("streaming data: \(pointToSend)\n - STRING: \(self.dataPointsArray)\n - DATA: \(self.dataPointsArray.description.data(using: String.Encoding.ascii)!)")
}
}
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 {
// send message to remote user that touches have ended
if self.streamIsEnabled == 0 {
// transmit any left over points
if self.dataPointsArray.count > 0 {
sendTouchPoints() // send touch data to remote user
clearSubLayers() // remove touches drawn to the screen
}
self.agoraKit.sendStreamMessage(self.dataStreamId, data: "touch-end".data(using: String.Encoding.ascii)!)
}
// 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