3

我正在使用陀螺仪在 iOS 上实现 360 度视频查看器,我需要为视频旋转两次,我使用了这个;

self.motionManager.deviceMotionUpdateInterval = 1.0/60.0;
    if (self.motionManager.isDeviceMotionAvailable) {
        CMDeviceMotion *deviceMotion = self.motionManager.deviceMotion;
        CMAttitude *currentAttitude = deviceMotion.attitude;
        //[self.motionManager startDeviceMotionUpdates];
        [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical];
    }

然后我做:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
CMDeviceMotion *currentDeviceMotion = self.motionManager.deviceMotion;
CMAttitude *currentAttitude = currentDeviceMotion.attitude;     
float roll  = currentAttitude.roll;
float pitch = currentAttitude.pitch;
float yaw = currentAttitude.yaw;

if (isLandscape)
{
    _player.rotation = -yaw + currentLocation.x;

    // 0 roll is phone flat, so we need to rotate -90 degrees to adjust vertical phone
    if (orientation == UIDeviceOrientationLandscapeLeft) // Invert roll and add 180 degrees offset
        _player.pitch = (roll - RADIAN_90_DEGREES + RADIAN_180_DEGREES) + currentLocation.y;
    else
        _player.pitch = -(roll - RADIAN_90_DEGREES) + currentLocation.y;

} else {
    if (orientation == UIDeviceOrientationFaceDown)
        _player.rotation = -yaw - currentLocation.x;
    else
        _player.rotation = -yaw + currentLocation.x;

    if (orientation == UIDeviceOrientationFaceUp)
        _player.pitch = (roll) + currentLocation.y;
    else if (orientation == UIDeviceOrientationFaceDown)
        _player.pitch = (roll) + currentLocation.y;
    else
        _player.pitch = -(roll) + currentLocation.y;
}

这在横向模式(左右)上效果很好,并且完全符合我的需要,我必须根据方向进行一些调整,但效果绝对好,它在纵向上我迷路了,我得到的所有值都与横向不同并且取决于肖像,FaceUp,FaceDown ......我很困惑,该怎么做,我研究并找到了旋转矩阵,四元数的参考......老实说,我已经尝试了很多东西,但在肖像上没有得到很好的结果. 任何帮助表示赞赏。

4

0 回答 0