0

我正在使用以下代码,但一直给我 0 值请检查我做错了什么

UILabel *rollLabel = [[UILabel alloc] init];
CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
float myRoll = radiansToDegrees(atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z)) ;
float myPitch = radiansToDegrees(atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z));
float myYaw = radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));
[rollLabel setText:[NSString stringWithFormat:@"ROLL = %f , - Pitch = %f, , - Yaw = %f", myRoll, myPitch, myYaw]];
rollLabel.frame = CGRectMake(0, 0, 400, 150);
rollLabel.backgroundColor=[UIColor greenColor];
rollLabel.textColor=[UIColor blackColor];
[self.view addSubview:rollLabel];

感谢您的帮助

4

1 回答 1

1

首先我会尝试如果所有对象都不为零,然后检查你是否真的运行了motionManager本身,比如:

_motionManager = [[CMMotionManager alloc] init];
_motionManager.deviceMotionUpdateInterval = 1.0/60;
[_motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical];

下一个问题可能是您的代码是否在主线程上运行

有很多选项可能会导致您遇到的问题

于 2017-05-10T12:23:29.863 回答