Ios 在iPhone上计算不同设备方向的偏航角?

Ios 在iPhone上计算不同设备方向的偏航角?,ios,iphone,sensors,quaternions,core-motion,Ios,Iphone,Sensors,Quaternions,Core Motion,在我的应用程序中,我需要在屏幕上显示一条水平线,因此我决定转到CMMotionManager。这是我的代码,非常简单: if ([_motionManager isDeviceMotionAvailable]) { if ([_motionManager isDeviceMotionActive] == NO) { [_motionManager setDeviceMotionUpdateInterval:0.1]; [_m

在我的应用程序中,我需要在屏幕上显示一条水平线,因此我决定转到
CMMotionManager
。这是我的代码,非常简单:

    if ([_motionManager isDeviceMotionAvailable]) {
        if ([_motionManager isDeviceMotionActive] == NO) {
            [_motionManager setDeviceMotionUpdateInterval:0.1];
            [_motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical toQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error){
                CMQuaternion quat = _motionManager.deviceMotion.attitude.quaternion;
                double yaw = asin(2*(quat.x*quat.z - quat.w*quat.y));
                // drawing line stuff.
            }];
        }
    } else {
        NSLog(@"Motion not avaliable!");;
    }
当我将手机置于向上纵向模式时,一切正常。但是由于
asin
方法只给了我一个(-pi/2,+pi/2)范围内的数字,如果我在横向模式或倒立纵向模式下拿着手机,我就无法画出一条完美的水平线,这条线只是停留在-pi/2或pi/2左右的角度,不会一直旋转

那么,我如何解决这个问题,有没有更通用的方法来处理四元数?提前谢谢