0

按照文档,这是我访问磁力计数据的尝试。不用说,它不起作用。

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

 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;

我错过了什么?

4

1 回答 1

1

这有效:

[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];
}];
于 2014-03-08T20:30:35.830 回答