在 ARCScene 中使用 Pan Gesture 移动节点

Using UIPanGesture to move node in ARScene

所以我正在尝试设置平移手势以将场景视图中的节点移动到视图的另一个区域。例如,如果我将杯子放在平面上,我希望能够将杯子移动到该表面上的另一个位置。到目前为止,我还没有看到其他问题,但仍然感到困惑。

@objc func panGesture(sender: UIPanGestureRecognizer){

    let sceneView = sender.view as! ARSCNView
    let pannedLocation = sender.location(in: sceneView)
    let hitTest = sceneView.hitTest(pannedLocation)

    let state = sender.state

    if(state == .failed || state == .cancelled){
        return
    }

    if(state == .began){
        let sceneHitTestResult = hitTest.first!
    }
}

查看此答案以获得完整的游乐场示例Drag Object in 3D View

您需要将此行添加到 viewDidLoad

sceneView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(panGesture(_:))))

基本的 panGesture 功能是...

@objc func panGesture(_ gesture: UIPanGestureRecognizer) {     

gesture.minimumNumberOfTouches = 1

let results = self.sceneView.hitTest(gesture.location(in: gesture.view), types: ARHitTestResult.ResultType.featurePoint)

guard let result: ARHitTestResult = results.first else {
    return
}

let hits = self.sceneView.hitTest(gesture.location(in: gesture.view), options: nil)
if let tappedNode = hits.first?.node {
    let position = SCNVector3Make(result.worldTransform.columns.3.x, result.worldTransform.columns.3.y, result.worldTransform.columns.3.z)
    tappedNode.position = position
 }

}