如何将手势位置转换为ARKit场景空间?

时间:2017-07-02 02:13:52

标签: swift arkit

我试图将SCNNode拖动到拖动位置的当前场景中。这是我的代码:

@objc func handlePan (gesture: UIPanGestureRecognizer) {

    var location = gesture.location(in: self.sceneView)

    switch gesture.state {
    case .began:
        if (redBlockUI?.frame.contains(location))! {
            currentBlock = createBlock(color: .red, location: location)
        }
        break;
    case .changed:
        if currentBlock != nil {
            let position = SCNVector3(location.x, location.y, -1)
            currentBlock?.position = position
        }
    case .ended:
        currentBlock = nil
    default:
        break;
    }
}


func createBlock (color: UIColor, location: CGPoint) -> SCNNode? {


    let block: SCNGeometry = SCNBox(width: 10.0, height: 10.0, length: 10.0, chamferRadius: 1.0)
    block.materials.first?.diffuse.contents = color

    let blockNode = SCNNode(geometry: block)
     blockNode.position = SCNVector3(location.x, location.y, -1)

    sceneView.scene.rootNode.addChildNode(blockNode)

    return blockNode
}

但是,此位置不会创建对象。我需要进行某种点转换吗?谢谢!

1 个答案:

答案 0 :(得分:0)