我正试图在下图中找到准确的角度(a)。
通过研究我明白我可以使用 CMotionManger 获得角度。我创建了一个单例类,它将为我提供有关设备移动的更新。获得" Pitch "值 CMQuaternion
为了更好地理解" Pitch"相对于轴,请参考下图。
这是我的代码,其中我得到了pitch的预期值。它是找到第一张图像中显示的角度的正确方法吗?
- (void) startMotionUpdate {
if (self.motionManager == nil) {
self.motionManager = [[CMMotionManager alloc] init];
}
self.motionManager.deviceMotionUpdateInterval = kUpdateInterval;
[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical
toQueue:self.deviceQueue
withHandler:^(CMDeviceMotion *motion, NSError *error)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CGFloat x = motion.gravity.x;
CGFloat y = motion.gravity.y;
CGFloat z = motion.gravity.z;
CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
double yaw = asin(2*(quat.x*quat.z - quat.w*quat.y));
DLog(@"Yaw ==> %f", yaw);
double myPitch = radiansToDegrees(atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z));
DLog(@"myPitch ==> %.2f degree", myPitch);
self.motionLastPitch = myPitch;
}];
}];
}
在日志中我得到这个印刷品:
[Line 64] myPitch ==> 74.71 degree
[Line 60] Yaw ==> -0.037314
[Line 64] myPitch ==> 74.68 degree
[Line 60] Yaw ==> -0.037849
[Line 64] myPitch ==> 74.67 degree
[Line 60] Yaw ==> -0.038531
[Line 64] myPitch ==> 74.69 degree
[Line 60] Yaw ==> -0.038637
[Line 64] myPitch ==> 74.71 degree
[Line 60] Yaw ==> -0.037314
[Line 64] myPitch ==> 74.68 degree
[Line 60] Yaw ==> -0.037849
[Line 64] myPitch ==> 75.65 degree
[Line 60] Yaw ==> -0.038531
[Line 64] myPitch ==> 76.90 degree
[Line 60] Yaw ==> -0.038637
我试图按照第一张图片放置手机,价值是否合适?如果是,那么如何获得Pitch的稳定值?
任何帮助/想法/指导都会有所帮助。
答案 0 :(得分:0)