如何使用手势旋转和缩放ARKit模型?

问题描述 投票:1回答:1

[我正在尝试像this video中那样旋转和缩放ARKit模型:

这是我的代码:

添加手势:

let tapGesure = UITapGestureRecognizer(target: self, action: #selector(handleTap))
sceneView.addGestureRecognizer(tapGesure)

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

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

以下是相应的方法:

点击手势:

// MARK: - Gesture Recognizers
@objc func handleTap(gesture: UITapGestureRecognizer) {

    let location = gesture.location(in: sceneView)
    guard let hitTestResult = sceneView.hitTest(location, types: .existingPlane).first else { return }
    let position = SCNVector3Make(hitTestResult.worldTransform.columns.3.x,
                                  hitTestResult.worldTransform.columns.3.y,
                                  hitTestResult.worldTransform.columns.3.z)
    addFoodModelTo(position: position)
}

[UIPanGestureRecognizer的手势]:>

@objc func moveNode(_ gesture: UIPanGestureRecognizer) {

    if !isRotating {

        //1. Get The Current Touch Point
        let currentTouchPoint = gesture.location(in: self.sceneView)

        //2. Get The Next Feature Point Etc
        guard let hitTest = self.sceneView.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
        baseNode.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)  
    }
}

使用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
            baseNode.eulerAngles.y = currentAngleY + rotation
        }

        //3. If The Gesture Has Ended Store The Last Angle Of The Cube
        if(gesture.state == .ended) {
            currentAngleY = baseNode.eulerAngles.y
            isRotating = false
        }
    }
}

但是它不起作用。我在做什么错?

[我正在尝试旋转和缩放此视频中的ARKit模型:这是我的代码:添加手势:let tapGesure = UITapGestureRecognizer(target:self,action:#selector(handleTap))sceneView ....

ios swift augmented-reality scenekit arkit
1个回答
1
投票

对于旋转手势,请执行以下方法:

override func viewDidLoad() {
    super.viewDidLoad()

    let rotationGesture = UIRotationGestureRecognizer(target: self, 
                                                      action: #selector(rotation))

    self.sceneView.addGestureRecognizer(rotationGesture)
}
© www.soinside.com 2019 - 2024. All rights reserved.