带有参考框架的CoreMotion iOS 5姿态不起作用 [英] CoreMotion iOS 5 attitude with reference frame doesn't work

查看:112
本文介绍了带有参考框架的CoreMotion iOS 5姿态不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试CoreMotion的新功能,首先可以设置参考框架,但是如果我使用DeviceMotionHandler并将参考框架设置为CMAttitudeReferenceFrameXTrueNorthZVertical,则输出为某些CMAttitudeReferenceFrameXArbitraryCorrectedZVertical.我始终以与办公桌相同的偏航角旋转iphone来启动应用程序,并且测试了不同的初始偏航角旋转,但结果始终相同.

i'm trying the new features of CoreMotion, above all the possibility to set the reference frame, but if i use a DeviceMotionHandler and the reference frame set to CMAttitudeReferenceFrameXTrueNorthZVertical the output is the some of CMAttitudeReferenceFrameXArbitraryCorrectedZVertical. i start the app with the iphone always in the same yaw rotation respect my desk, and i test different initial yaw rotation, but the result is always the same.

motionManager = [[CMMotionManager alloc] init];
motionManager.showsDeviceMovementDisplay = YES;
motionManager.deviceMotionUpdateInterval = 1.0/60.0;    

CMDeviceMotionHandler  motionHandler = ^ (CMDeviceMotion *motion, NSError *error) {
    NSLog(@"%f      %f         %f", motion.attitude.pitch, motion.attitude.roll, motion.attitude.yaw);
};

[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical toQueue:[NSOperationQueue currentQueue] withHandler:motionHandler];

我找到了解决我问题的方法,但是我不明白为什么以前的代码不起作用.我仅在motionHandler中添加一个CMAttitude变量* a.

i found the solution of my problem, but i can't understand why the previous code doesn't work. I add just a CMAttitude variable *a in motionHandler.

- (void)viewDidLoad
{
[super viewDidLoad];
motionManager = [[CMMotionManager alloc] init];
motionManager.showsDeviceMovementDisplay = YES;
motionManager.deviceMotionUpdateInterval = 1.0/60.0;    

CMDeviceMotionHandler  motionHandler = ^ (CMDeviceMotion *motion, NSError *error) {
    CMAttitude *a = motionManager.deviceMotion.attitude;
    labelAngle.text = [NSString stringWithFormat:@"%f      %f         %f",a.pitch, a.roll,a.yaw];
    labelAngle2.text = [NSString stringWithFormat:@"%f       %f      %f", motion.attitude.pitch, motion.attitude.roll, motion.attitude.yaw];
};

[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical toQueue:[NSOperationQueue currentQueue] withHandler:motionHandler];}

推荐答案

我认为这是因为..如果您首先定义处理程序,则运动对象的态度属性已经设置为默认值.稍后在您自己的代码中,态度属性变为只读.因此,当您使用此处理程序启动运动更新时,将无法再更改运动的姿态属性.但是motionManager.deviceMotion的态度属性设置为您在startDeviceMotionUpdatesUsingReferenceFrame中指定的值,并且当您使用startDeviceMotionUpdatesUsingReferenceFrame开始运动更新时,这将被读入一个对象.现在,对象具有正确的姿态,而运动对象具有默认的姿态.

I think this is because .. if you define the handler first, the attitude property of your motion object has already been set to the default value. Later in your own code this attitude property becomes read only. So, when you start the motion updates with this handler the attitude property of motion no longer can be changed. But the attitude property of motionManager.deviceMotion is set to whatever you specify in startDeviceMotionUpdatesUsingReferenceFrame and this is being read into the a object when you start the motion updates with startDeviceMotionUpdatesUsingReferenceFrame. The a object now has the correct attitude, while the motion object has the default attitude.

这篇关于带有参考框架的CoreMotion iOS 5姿态不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆