我正在尝试 CoreMotion 的新功能,最重要的是设置引用系的可能性,但如果我使用 DeviceMotionHandler 并将引用系设置为 CMAttitudeReferenceFrameXTrueNorthZVertical,则输出是 CMAttitudeReferenceFrameXArbitraryCorrectedZVertical 的一部分。 我用 iphone 启动应用程序时总是以相同的偏航旋转尊重我的 table ,我测试了不同的初始偏航旋转,但结果总是一样的。
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];}
最佳答案
我认为这是因为.. 如果您先定义处理程序,则运动对象的态度属性已经设置为默认值。稍后在您自己的代码中,此态度属性变为只读。因此,当您使用此处理程序开始运动更新时,运动的姿态属性将不再可以更改。但是 motionManager.deviceMotion 的态度属性设置为您在 startDeviceMotionUpdatesUsingReferenceFrame 中指定的任何值,并且当您使用 startDeviceMotionUpdatesUsingReferenceFrame 开始运动更新时,它会被读入对象。 a 对象现在具有正确的姿态,而 motion 对象具有默认姿态。
关于iphone - 带有引用框架的 CoreMotion iOS 5 态度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7918091/