CoreMotion iOS 5 姿态与参考系不起作用

发布于 2024-12-12 09:06:34 字数 1645 浏览 0 评论 0原文

我正在尝试 CoreMotion 的新功能,首先是设置参考系的可能性,但如果我使用 DeviceMotionHandler 并将参考系设置为 CMAttitudeReferenceFrameXTrueNorthZVertical,则输出是 CMAttitudeReferenceFrameXArbitraryCorrectedZVertical 的一些内容。 我用 iPhone 启动应用程序,始终以相同的偏航旋转尊重我的办公桌,并且我测试了不同的初始偏航旋转,但结果始终相同。

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。

- (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];}

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];

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];}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2024-12-19 09:06:40

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文