我正在尝试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];
}; …
Run Code Online (Sandbox Code Playgroud) 分析deviceMotion.timestamp我发现DeviceMotion中设置的更新频率不是实际的更新频率.
我实现了一个应用程序,以便测试,低于我所看到的!
update frequency actual frequency average time between two calls
1/10.000000 10.232265 0.097730
1/20.000000 19.533729 0.051194
1/30.000000 30.696613 0.032577
1/40.000000 42.975122 0.023269
1/50.000000 53.711000 0.018618
1/60.000000 53.719106 0.018615
1/70.000000 71.627016 0.013961
1/80.000000 71.627263 0.013961
1/90.000000 53.719365 0.018615
1/100.000000 107.442667 0.009307
1/110.000000 107.437022 0.009308
Run Code Online (Sandbox Code Playgroud)
有人注意到了同样的事情吗?这是一个错误吗?