Bat*_*tti 6 iphone objective-c core-motion ios5
我正在尝试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];
Run Code Online (Sandbox Code Playgroud)
我找到了我的问题的解决方案,但我无法理解为什么以前的代码不起作用.我在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];}
Run Code Online (Sandbox Code Playgroud)
我认为这是因为..如果您首先定义处理程序,则运动对象的态度属性已设置为默认值。稍后在您自己的代码中,该态度属性将变为只读。因此,当您使用此处理程序启动运动更新时,运动的姿态属性不再可以更改。但是,motionManager.deviceMotion 的姿态属性设置为您在 startDeviceMotionUpdatesUsingReferenceFrame 中指定的任何内容,并且当您使用 startDeviceMotionUpdatesUsingReferenceFrame 启动运动更新时,该属性将被读入对象中。现在,a 对象具有正确的姿态,而运动对象具有默认姿态。