检测ARKit中的SCNNode上的触摸

时间:2018-02-18 16:50:47

标签: scenekit arkit scnnode

我已将一个SCNNode(一个平面)放置在ARKit 1.5 beta中识别图像的位置。我想在点击飞机时向控制台打印一条消息。到目前为止,我有这段代码:

// 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.
             */
            //planeNode.runAction(self.imageHighlightAction)

            // Add the plane visualization to the scene.
            node.addChildNode(planeNode)
        }

        DispatchQueue.main.async {
            let imageName = referenceImage.name ?? ""
            self.statusViewController.cancelAllScheduledMessages()
            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 {
                return
            }
            if let planeNode = planeNode, planeNode == result.node {
                print("match")
            }

        }
    }

但是我在这一行得到了一个'未解析的标识符'错误:if let planeNode = planeNode, planeNode == result.node {,我理解这是因为planeNode是在上面的Renderer函数中定义的,并且不在正确的范围内。我的问题是如何解决这个问题,因为我不相信我可以在Renderer中返回值,也不能将touchesBegan函数放在Renderer函数中,因此它在正确的范围内。谁能给我任何关于如何解决这个问题的想法?谢谢!

2 个答案:

答案 0 :(得分:2)

要解决此问题,请在ViewController中声明一个全局变量

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!

    var planeNode : SCNNode? = nil

    .....

}

然后使用刚刚声明的planeNode变量,并在渲染器方法中将其分配给实际的planeNode

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

     .........

     let planeNode = SCNNode(geometry: plane)
     self.planeNode = planeNode
}

这样,您可以从类内部的任何地方访问planeNode

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 {
            return
        }
        if let planeNode = planeNode, planeNode == result.node {
            print("match")
        }

    }
}

答案 1 :(得分:0)

// Add a gesture recognizer
 let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
    self.sceneView.addGestureRecognizer(tapGestureRecognizer)
    tapGestureRecognizer.cancelsTouchesInView = false 

@objc func handleTap(sender:UITapGestureRecognizer){

    guard let sceneView = sender.view as? ARSCNView else {return}
    let touchLcoation = sender.location(in: sceneView)
    let hitTestResult = sceneView.hitTest(touchLcoation, types: .existingPlaneUsingExtent)

    if !hitTestResult.isEmpty {
        // add something to scene 
        // e.g self.sceneView.scene.rootNode.addChildNode(yourNode!)
    }
}