我有一个关于移动相机 up/down 和 left/right 而不在 SceneKit 中使用平移手势旋转的问题。我发现有很多关于旋转相机的讨论,我正在使用它进行旋转。但我无法弄清楚如何在不旋转的情况下移动相机。我试图模仿,allowCameraControl 用户可以用两根手指平移来改变相机的 x 和 y 位置。这是我的手势识别器的代码,任何帮助将不胜感激,谢谢!
func handlePan(gestureRecognize: UIPanGestureRecognizer) {
let numberOfTouches = gestureRecognize.numberOfTouches
var translation = gestureRecognize.translation(in: gestureRecognize.view!)
var widthRatio = Float(translation.x) / Float(gestureRecognize.view!.frame.size.width) + lastWidthRatio
var heightRatio = Float(translation.y) / Float(gestureRecognize.view!.frame.size.height) + lastHeightRatio
if (numberOfTouches==fingersNeededToPan) {
self.cameraOrbit.eulerAngles.y = Float(-2 * M_PI) * widthRatio
self.cameraOrbit.eulerAngles.x = Float(-M_PI) * heightRatio
//for final check on fingers number
lastFingersNumber = fingersNeededToPan
}
if numberOfTouches == 2 {
self.cameraNode.position.x = -(Float(translation.x) / Float(gestureRecognize.view!.frame.size.width) + lastWidthRatio)
self.cameraNode.position.y = -(Float(translation.y) / Float(gestureRecognize.view!.frame.size.height) + lastHeightRatio)
}
lastFingersNumber = (numberOfTouches>0 ? numberOfTouches : lastFingersNumber)
if (gestureRecognize.state == .ended && lastFingersNumber==fingersNeededToPan) {
lastWidthRatio = widthRatio
lastHeightRatio = heightRatio
}
}
2 回答
如果要滑动 left/right 或 up/down,则不需要
cameraOrbit
节点(这是Scenekit 相机绕物体运行使用的节点)。相反,您只想滑动 left/right(X 轴)或 up/down(Y 轴)以响应触摸。看起来你正在这样做。但是如果
cameraNode
是cameraOrbit
的孩子,你将会移动cameraOrbit
的坐标系,这也是你的手势处理器旋转的!使摄像机节点成为场景根节点的子节点。现在,当您的相机没有与根坐标系对齐时,会出现混乱的事情。如果摄像机的 X,Y 和 Z 轴与场景的 X/Y/Z 轴平行,则可以调整摄像机的 X/Y 位置以进行移动。但如果相机已旋转或指向 Z 轴,则需要调整相机节点的
transform
,以便在相机平面内移动 left/right 或 up/down。我会尽快扩展这个答案以证明这一点。现在你可以很简单地做到: