the docs之后,这是我尝试访问磁力计数据的尝试。不用说,它不起作用。

我已经使用完全相同的方法来使陀螺仪和加速度计数据工作,但是由于某种原因,我使用该轴获得了每个轴上的全零。

 motionManager = [[CMMotionManager alloc] init];


[motionManager startDeviceMotionUpdates];
[motionManager startMagnetometerUpdatesToQueue:
 [NSOperationQueue currentQueue] withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
     double x = motionManager.deviceMotion.magneticField.field.x;
     double y = motionManager.deviceMotion.magneticField.field.y;
     double z = motionManager.deviceMotion.magneticField.field.z;

     self.magnetometerDataLabel.text = [NSString stringWithFormat:@"{%8.4f, %8.4f, %8.4f}", x, y, z];
 }];
motionManager.magnetometerUpdateInterval = 1.0 / 60.0;


我想念什么?

最佳答案

这工作:

[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical toQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
    double x = motionManager.deviceMotion.magneticField.field.x;
    double y = motionManager.deviceMotion.magneticField.field.y;
    double z = motionManager.deviceMotion.magneticField.field.z;

    self.magnetometerDataLabel.text = [NSString stringWithFormat:@"{%8.4f, %8.4f, %8.4f}", x, y, z];
}];

10-07 17:43