I'm playing around with the ARKit and image detection. Now I have an app that detects images an places planes on the screen where the detected objects are.
How can I add a clickable element like a button on the planes. I want to have a click event on each detected object.
This is what my renderer function looks:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor else { return }
let referenceImage = imageAnchor.referenceImage
updateQueue.async {
let plane = SCNPlane(width: referenceImage.physicalSize.width,
height: referenceImage.physicalSize.height)
let planeNode = SCNNode(geometry: plane)
planeNode.opacity = 0.25
planeNode.eulerAngles.x = -.pi / 2
planeNode.runAction(self.imageHighlightAction)
node.addChildNode(planeNode)
}
DispatchQueue.main.async {
let imageName = referenceImage.name ?? ""
self.statusViewController.cancelAllScheduledMessages()
// self.statusViewController.showMessage("Detected image “\(imageName)”")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let second = storyboard.instantiateViewController(withIdentifier: "InfoViewController")as! InfoViewController
second.myStringValue = imageName
// self.navigationController?.pushViewController(second, animated: true)
}
}
There are a few ways you can approach this:
1: Standard Approach:
In your
delegate
method, assign each node a name as per below (clearly if you have lots of nodes you would want to store them in an Array or Dictionary depending upon your needs):Then using
touchesBegan
, perform a hitTest and handle your touch accordingly:2: An Interesting Approach:
UIKit
elements can actually be added as anSCNGeometry's Material
. I personally haven't seen many people use this approach, but it may prove useful to anyone who wants to incorporateUIKit
withARKit
.Create A Custom UIButton e.g:
And in your delegate method do the following:
This should get you started...