ios - ARKit - How to create rotate object gesture function?

前端 未结 1 526
借酒劲吻你
借酒劲吻你 2021-02-03 15:01

I\'m new in ARKit. I want to create a function to rotate object. This is my code about drag and rotate object:

// Rotate object
@objc func rotateRecognized(sende         


        
相关标签:
1条回答
  • 2021-02-03 16:01

    I think it would be best to approach this using GestureRecognizers rather than a combination of both touches and gestures together.

    Let's have a look therefore, at how we could tackle this.

    You already have the functionality to drag an SCNNode which could quite easily be converted into a UIPanGesture, and you want a function to rotate an SCNNode around it's YAxis which we can quite easily do with a UIRotationGestureRecognizer.

    In my example I have an SCNNode called currentNode, although yours will of course be different.

    First we will create two variables:

    //Store The Rotation Of The CurrentNode
    var currentAngleY: Float = 0.0
    
    //Not Really Necessary But Can Use If You Like 
    var isRotating = false
    

    Then we will create two gestureRecognizers in viewDidLoad:

     let panGesture = UIPanGestureRecognizer(target: self, action: #selector(moveNode(_:)))
     self.view.addGestureRecognizer(panGesture)
    
     let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotateNode(_:)))
     self.view.addGestureRecognizer(rotateGesture)
    

    Having done this, we then need to create our functions.

    The first will handle dragging the currentNode e.g:

    /// Rotates An Object On It's YAxis
    ///
    /// - Parameter gesture: UIPanGestureRecognizer
    @objc func moveNode(_ gesture: UIPanGestureRecognizer) {
    
        if !isRotating{
    
        //1. Get The Current Touch Point
        let currentTouchPoint = gesture.location(in: self.augmentedRealityView)
    
        //2. Get The Next Feature Point Etc
        guard let hitTest = self.augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }
    
        //3. Convert To World Coordinates
        let worldTransform = hitTest.worldTransform
    
        //4. Set The New Position
        let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)
    
        //5. Apply To The Node
        currentNode.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)
    
        }
    }
    

    The second rotating it around it's YAxis:

    /// Rotates An SCNNode Around It's YAxis
    ///
    /// - Parameter gesture: UIRotationGestureRecognizer
    @objc func rotateNode(_ gesture: UIRotationGestureRecognizer){
    
        //1. Get The Current Rotation From The Gesture
        let rotation = Float(gesture.rotation)
    
        //2. If The Gesture State Has Changed Set The Nodes EulerAngles.y
        if gesture.state == .changed{
            isRotating = true
            currentNode.eulerAngles.y = currentAngleY + rotation
        }
    
        //3. If The Gesture Has Ended Store The Last Angle Of The Cube
        if(gesture.state == .ended) {
            currentAngleY = currentNode.eulerAngles.y
            isRotating = false
        }
    }
    

    This is a very crude example and you would need to look at the different gestureStates etc, but hopefully it should point you in the right direction...

    0 讨论(0)
提交回复
热议问题