我是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方法,该对象无法旋转。 如何解决这个问题?谢谢。