在场景视图中拖放对象-ARKit

时间:2018-07-19 14:41:32

标签: drag-and-drop scenekit arkit

我正在开发一个需要在场景视图中拖放对象的应用程序。当所有必需的对象均被移动并标记为完成时,这是一种审核应用程序。我遇到了一个使用UIPanGestureRecognizer的示例。不幸的是,当使用其他手势事件(例如捏和轻击)时,拖放运动不是很平稳。我也需要使用其他事件(捏,轻按)来调整对象的大小。以下是代码段。我们真的很感激改善机芯的任何帮助,因为我一直被这个问题困扰。

///存储来自hittest的先前坐标以与当前坐标进行比较

var PCoordx: Float = 0.0

var PCoordz: Float = 0.0

@objc func move(_ gestureRecognizer: UIPanGestureRecognizer){

    if gestureRecognizer.state == .began{

        let hitNode = sceneView.hitTest(gestureRecognizer.location(in: sceneView), options: nil)

        if hitNode.first?.node.name != "structure" {

            if hitNode.first?.worldCoordinates.x != nil {PCoordx = (hitNode.first?.worldCoordinates.x)!}

            if hitNode.first?.worldCoordinates.z != nil {PCoordz = (hitNode.first?.worldCoordinates.z)!}

        }

    }

    // when you start to pan in screen with your finger

    // hittest gives new coordinates of touched location in sceneView

    // coord-pcoord gives distance to move or distance paned in sceneview

    if gestureRecognizer.state == .changed {

        let hitNode = sceneView.hitTest(gestureRecognizer.location(in: sceneView), options: nil)

        if let coordx = hitNode.first?.worldCoordinates.x{

            if let coordz = hitNode.first?.worldCoordinates.z{

                let action = SCNAction.moveBy(x: CGFloat(coordx-PCoordx), y: 0, z: CGFloat(coordz-PCoordz), duration: 0.1)

                      let hit = hitNode.first?.node

                   if hit?.name == "cylinder" {

                    selectedNode = cylinder

                }

                    if hit?.name  == "cone" {

                    selectedNode = cone

                }

                    if hit?.name  == "blanket" {

                    selectedNode = blanket

                }

                    selectedNode = hit

                     selectedNode.runAction(action)

                   PCoordx = coordx

                PCoordz = coordz

            }

        }

              gestureRecognizer.setTranslation(CGPoint.zero, in: sceneView)

    }

    if gestureRecognizer.state == .ended{

        PCoordx = 0

        PCoordz = 0

    }

}

我非常感谢所有帮助。谢谢!!

0 个答案:

没有答案