【发布时间】:2019-06-21 21:59:53
【问题描述】:
我想创建一个示例应用程序,允许用户在点击地球上的大陆时获取有关大陆的信息。为了做到这一点,我需要找出用户在场景(SceneKit)中点击 SCNSphere 对象的位置。我试图这样做:
import UIKit
import SceneKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene()
/* Lighting and camera added (hidden)*/
let earthNode = SCNSphere(radius: 1)
/* Added styling to the Earth (hidden)*/
earthNode.name = "Earth"
scene.rootNode.addChildNode(earthNode)
let sceneView = self.view as! SCNView
sceneView.scene = scene
sceneView.allowsCameraControl = true
// add a tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
sceneView.addGestureRecognizer(tapGesture)
}
@objc func handleTap(_ gestureRecognize: UIGestureRecognizer) {
// retrieve the SCNView
let sceneView = self.view as! SCNView
// check what nodes are tapped
let p = gestureRecognize.location(in: scnView)
let hitResults = sceneView.hitTest(p, options: [:])
// check that we clicked on at least one object
if hitResults.count > 0 {
// retrieved the first clicked object
let result: SCNHitTestResult = hitResults[0]
print(result.node.name!)
print("x: \(p.x) y: \(p.y)") // <--- THIS IS WHERE I PRINT THE COORDINATES
}
}
}
但是,当我实际运行此代码并单击球体上的某个区域时,它会在屏幕上打印出点击的坐标,而不是我在球体上点击的位置。例如,当我点击球体的中心时,坐标是相同的,当我在旋转球体后再次点击球体的中心时。
我想知道我在实际球体上按下的位置,而不仅仅是我在屏幕上单击的位置。我应该解决这个问题的最佳方法是什么?
【问题讨论】:
标签: ios swift uigesturerecognizer scenekit scnsphere