【发布时间】:2018-07-28 23:29:38
【问题描述】:
我在 ARKit 1.5 beta 中已识别图像的位置放置了一个 SCNNode(一个平面)。当飞机被点击时,我想在控制台上打印一条消息。到目前为止,我有这个代码:
// 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 函数中,使其在正确的范围内。谁能给我有关如何解决此问题的任何想法?谢谢!
【问题讨论】:
-
要解决这个问题,您需要了解局部变量和全局变量之间的区别:developer.apple.com/library/content/documentation/Swift/… 这与 Scenekit 或 ARkit 无关,但非常基本的编程。或者试试这个:youtube.com/watch?v=qRZAdbAgj3c
-
感谢这个。你是最棒的!