gpt4 book ai didi

ios - ARKit - 如何创建旋转对象手势功能?

转载 作者:搜寻专家 更新时间:2023-10-30 22:29:21 24 4
gpt4 key购买 nike

我是 ARKit 的新手。我想创建一个旋转对象的函数。这是我关于拖动和旋转对象的代码:

// Rotate object
@objc func rotateRecognized(sender: UIPanGestureRecognizer) {
let sceneView = sender.view as! ARSCNView
let swipeLocation = sender.location(in: sceneView)
let hitTest = sceneView.hitTest(swipeLocation)
if !hitTest.isEmpty {
sender.minimumNumberOfTouches = 2
let results = hitTest.first!
let node = results.node
let xPan = sender.velocity(in: sceneView).x/10000
node.runAction(SCNAction.rotateBy(x: 0, y: xPan, z: 0, duration: 0.1))
}
}

// Drag object
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
//1. Get The Current Touch Point
guard let currentTouchPoint = touches.first?.location(in: self.sceneView),
//2. Get The Next Feature Point Etc
let hitTest = 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
self.sceneView.scene.rootNode.enumerateChildNodes{ (node, _) in
node.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)
}

}

当我拖动一个对象时,它工作正常。但是当我用两根手指滑动旋转对象时,对象不能旋转,直到我删除 touchesMoves 方法。如何解决这个问题?谢谢。

最佳答案

我认为最好使用 GestureRecognizers 来解决这个问题而不是两者的组合 touchesgestures在一起。

因此,让我们看看如何解决这个问题。

您已经拥有拖动 SCNNode 的功能,它可以很容易地转换为 UIPanGesture ,并且您想要一个函数来围绕它的 YAxis 旋转 SCNNode,我们可以很容易地使用 UIRotationGestureRecognizer 来做到这一点。 .

在我的示例中,我有一个名为 currentNode 的 SCNNode,尽管您的当然会有所不同。

首先我们将创建两个变量:

//Store The Rotation Of The CurrentNode
var currentAngleY: Float = 0.0

//Not Really Necessary But Can Use If You Like
var isRotating = false

然后我们将创建两个gestureRecognizersviewDidLoad :

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

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

完成此操作后,我们需要创建我们的函数。

第一个将处理拖动当前节点,例如:

/// 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)

}
}

第二个绕 Y 轴旋转:

/// 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
}
}

这是一个非常粗略的示例,您需要查看不同的 gestureStates等,但希望它能为您指明正确的方向...

关于ios - ARKit - 如何创建旋转对象手势功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com