Dragging SCNNode in ARKit Using SceneKit

前端 未结 3 622
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 11:45

I have a simple SCNNode in ARKit and I am trying to drag it wherever I moved my finger on the phone. Here is my code.

 @objc func pan(recognizer :UIGestureR         


        
3条回答
  •  囚心锁ツ
    2020-12-08 12:39

    I handle translation with PanGesture like this. The division by 700 is to smooth and adjust speed of movement, I reached to that value by trial or error, you may want to experiment with it

    @objc func onTranslate(_ sender: UIPanGestureRecognizer) {
        let position = sender.location(in: scnView)
        let state = sender.state
    
        if (state == .failed || state == .cancelled) {
            return
        }
    
        if (state == .began) {
            // Check it's on a virtual object
            if let objectNode = virtualObject(at: position) {
                // virtualObject(at searches for root node if it's a subnode
                targetNode = objectNode
                latestTranslatePos = position
            }
    
        }
        else if let _ = targetNode {
    
            // Translate virtual object
            let deltaX = Float(position.x - latestTranslatePos!.x)/700
            let deltaY = Float(position.y - latestTranslatePos!.y)/700
    
            targetNode!.localTranslate(by: SCNVector3Make(deltaX, 0.0, deltaY))
    
            latestTranslatePos = position
    
            if (state == .ended) {
                targetNode = nil
            }
        }
    }
    

提交回复
热议问题