【发布时间】:2025-12-25 19:40:17
【问题描述】:
我有一个关于在不使用平移手势在 SceneKit 中旋转的情况下向上/向下和向左/向右移动相机的问题。我发现很多关于旋转相机的讨论,我用它来旋转。但是我不知道如何在不旋转相机的情况下移动它。我试图模仿,allowsCameraControl,用户可以用两根手指平移来改变相机的 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
}
}
【问题讨论】:
-
澄清一下:你想在不改变摄像机角度的情况下横向移动吗?例如。我面向东北,我一边走一边(向左西北,向右东南),同时继续面向东北?
-
是的,我只是想在不旋转的情况下向左/向右或向上/向下移动相机,就像使用 allowCameraControl 一样,对不起@HalMueller