【问题标题】:Using UIPanGesture to move node in ARScene使用 UIPanGesture 在 ARScene 中移动节点
【发布时间】:2018-01-26 17:48:10
【问题描述】:

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

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

【问题讨论】:

  • 这是我的回答:link

标签: ios swift scenekit arkit


【解决方案1】:

查看此答案以获取完整的游乐场示例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
 }

} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多