使用UIPanGesture在ARScene中移动节点

时间:2018-01-26 04:51:17

标签: ios swift scenekit arkit

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

@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!
    }
}

1 个答案:

答案 0 :(得分:2)

查看完整游乐场示例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
 }

}