ARKit图像跟踪-添加平移手势

时间:2019-05-15 13:36:01

标签: ios swift arkit

我正在开发具有图像跟踪功能的arkit应用程序。当相机指向图像时,我的对象就会出现。我希望能够在图像周围移动对象,因此我添加了平移手势,但是它不起作用。我试图移动对象,但它没有移动。

var selectedObject: String = "model1"

override func viewDidLoad() {
    super.viewDidLoad()
    sceneview.delegate = self
    let  config = ARWorldTrackingConfiguration()

    if let imagesToTrack = ARReferenceImage.referenceImages(inGroupNamed: "marker", bundle: Bundle.main){

        config.detectionImages = imagesToTrack
        config.maximumNumberOfTrackedImages = 1
    }

    sceneview.session.run(config)
    sceneview.autoenablesDefaultLighting = true
    sceneview.automaticallyUpdatesLighting = true
    registerGestureRecognizers()
}

func renderer(_ renderer: SCNSceneRenderer, didAdd newNode: SCNNode, for anchor: ARAnchor) {
    if let imageAnchor = anchor as? ARImageAnchor{
        if (imageAnchor.referenceImage.name == "ar_split"){
            if let airplaneScene = SCNScene(named: "art.scnassets/\(selectedObject).dae"){

                if let airplaneNode = airplaneScene.rootNode.childNodes.first {
                    currentNode = airplaneNode
                    rootNode = newNode
                    rootNode!.addChildNode(airplaneNode)
                }
            }
        }

    }
}
func registerGestureRecognizers(){
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(pan))
    self.sceneview.addGestureRecognizer(panGestureRecognizer)

}
@objc func pan(sender: UIPanGestureRecognizer){
    let panLocation = sender.location(in: sceneview)
    let hitTest = sceneview.hitTest(panLocation)

    var node: SCNNode = SCNNode()

    if (!hitTest.isEmpty){
        let result = hitTest.first!
        node = result.node
    }

    if sender.state == .changed {
        let newLocation = sender.location(in: sceneview)
        let newHitTest = sceneview.hitTest(newLocation)
        if (!newHitTest.isEmpty){
            let positionColumn = newHitTest.first!.worldCoordinates
            node.position = SCNVector3(positionColumn.x, node.position.y, positionColumn.z)
        }
    }
}

我在做错什么,如何向图像跟踪应用添加平移手势?

0 个答案:

没有答案