Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Detect touch on SCNNode in ARKit

AlanR917AlanR917 Posts: 66Registered Users @
edited February 18 in iOS SDK Development Forums
Hi, I have placed a SCNNode (a plane) at the location of a recognized image in ARKit 1.5 beta. I would like to print a message to the console when the plane is tapped on. So far I have this code:
// MARK: - ARSCNViewDelegate (Image detection results)
    /// - Tag: ARImageAnchor-Visualizing
    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

        guard let imageAnchor = anchor as? ARImageAnchor else { return }
        let referenceImage = imageAnchor.referenceImage
        updateQueue.async {

            // Create a plane to visualize the initial position of the detected image.
            let plane = SCNPlane(width: referenceImage.physicalSize.width,
                                 height: referenceImage.physicalSize.height)
            let planeNode = SCNNode(geometry: plane)
            planeNode.opacity = 0.25

             `SCNPlane` is vertically oriented in its local coordinate space, but
             `ARImageAnchor` assumes the image is horizontal in its local space, so
             rotate the plane to match.
            planeNode.eulerAngles.x = -.pi / 2

             Image anchors are not tracked after initial detection, so create an
             animation that limits the duration for which the plane visualization appears.

            // Add the plane visualization to the scene.

        DispatchQueue.main.async {
            let imageName = ?? ""
            self.statusViewController.showMessage("Detected image “\(imageName)”")

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first as! UITouch
        if(touch.view == self.sceneView){
            print("touch working")
            let viewTouchLocation:CGPoint = touch.location(in: sceneView)
            guard let result = sceneView.hitTest(viewTouchLocation, options: nil).first else {
            if let planeNode = planeNode, planeNode == result.node {


But I'm getting an 'unresolved identifier' error on line 47, which I understand because planeNode is defined in the Renderer function above and is not in the right scope. My question is how to fix this, because I dont believe I can return the value in Renderer, nor can I put the touchesBegan function in the Renderer function so its in the right scope. Can anyone give me any ideas on how to fix this? Thanks!
Sign In or Register to comment.