Jos*_*mez 5 portrait objective-c ios devicemotion 360-virtual-reality
我正在使用陀螺仪在 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];
}
Run Code Online (Sandbox Code Playgroud)
然后我这样做:
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;
}
Run Code Online (Sandbox Code Playgroud)
这在横向模式(左和右)上效果很好,并且完全符合我的需要,我必须根据方向进行一些调整,但效果绝对很好,在纵向模式下我迷失了,我得到的所有值与横向模式有很大不同取决于肖像,FaceUp,FaceDown...我很困惑,该怎么办,我研究并找到了旋转矩阵,四元数的参考...老实说,我已经尝试了很多东西,但在肖像上没有得到好的结果。任何帮助表示赞赏。