Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Scenekit中移动摄影机_Ios_Swift_Scenekit - Fatal编程技术网

Ios 在Scenekit中移动摄影机

Ios 在Scenekit中移动摄影机,ios,swift,scenekit,Ios,Swift,Scenekit,我有一个问题,关于在SceneKit中不使用平移手势旋转的情况下上/下和左/右移动相机。我发现了很多关于旋转相机的讨论,我用它来旋转。但是我不知道如何在不旋转相机的情况下移动它。我试着模仿,AllowCameraControl,用户可以用两个手指平移来改变相机的x和y位置。这是我的平移手势识别器的代码,任何帮助都将不胜感激,谢谢 func handlePan(gestureRecognize: UIPanGestureRecognizer) { let numberOfTouches

我有一个问题,关于在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
    }
}

如果要向左/向右或向上/向下滑动,则不需要使用
cameraOrbit
节点(这就是它的用途)

相反,您只需要根据触摸向左/向右(X轴)或向上/向下(Y轴)滑动。看起来你正在这么做。但是如果
cameraNode
cameraOrbit
的子对象,则您将在
cameraOrbit
的坐标系中移动,该坐标系也将由手势处理程序旋转!使摄影机节点成为场景根节点的子节点


现在,当你的相机没有与根坐标系对齐时,会发生令人困惑的事情。如果摄影机的X、Y和Z轴与场景的X/Y/Z轴平行,则可以调整摄影机的X/Y位置以移动。但是,如果相机已旋转,或指向Z轴之外,则需要调整相机节点的
变换
,以便在相机平面中左/右或上/下移动。我将尝试在不久的将来扩展这个答案,以证明这一点。

现在您可以做得非常简单:

scnView.allowsCameraControl = true
scnView.defaultCameraController.interactionMode = .orbitTurntable
scnView.defaultCameraController.inertiaEnabled = true
scnView.defaultCameraController.maximumVerticalAngle = 89
scnView.defaultCameraController.minimumVerticalAngle = -89

澄清一下:你想在不改变摄像机角度的情况下侧向移动吗?例如,我面朝东北,在继续面朝东北的同时侧身(从西北向左,从东南向右)走?是的,我只是试图在不旋转的情况下向左/向右或向上/向下移动相机,就像AllowCameraControl一样,很抱歉给您带来混淆@HalMueller