1
我在viewDidLoad
如何使用平移手勢識別器移動ARSCNView中的節點?
let scene = SCNScene(named: "funModel")!
myNode = scene.rootNode.childNode(withName: "blah")!
myNode.position = SCNVector3Make(0, 0, -1)
sceneView.scene = scene
let pan = UIPanGestureRecognizer(target: self, action: #selector(panGesture(recognizer:)))
view.addGestureRecognizer(pan)
下面的代碼,然後從another SO answer在panGesture
下面的代碼,但它不工作。節點只是看起來像0,0,0。我只是想能夠在屏幕上拖動節點。
let projectedOrigin = sceneView.projectPoint(SCNVector3Zero)
let vp = recognizer.location(in: sceneView)
let vpWithZ = SCNVector3(x: Float(vp.x), y: Float(vp.y), z: Float(projectedOrigin.z))
let worldPoint = sceneView.unprojectPoint(vpWithZ)
myNode.position = worldPoint
請注意,所述其他答案特定於沿垂直於相機的假想平面移動節點;如果這不是你的意圖,你會得到你不想要的結果。 – rickster