0

我最近在做一个指南针,涉及到CoreMotion框架,但是查了半天没找到关于如何实现这个框架的指南针效果的资料,希望能得到大家的帮助。

4

2 回答 2

2
于 2011-05-17T11:57:10.067 回答
0

我的“Helloworld”CoreMotion:

1)添加CoreMotion.framework

2)在.h文件中:

#import <CoreMotion/CoreMotion.h>

3) 在 .m 文件中:

CMMotionManager *myMotionManager= [[CMMotionManager alloc] init];
myMotionManager.deviceMotionUpdateInterval = 1/60;
[myMotionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical  toQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
     double x = myMotionManager.deviceMotion.magneticField.field.x;
     double y = myMotionManager.deviceMotion.magneticField.field.y;
     double z = myMotionManager.deviceMotion.magneticField.field.z;
    NSLog(@"Strength of Earth's magnetic field= %8.2f",sqrt(x*x+y*y+z*z));
    myLabel.text = [NSString stringWithFormat:@"%8.2f", sqrt(x*x+y*y+z*z)];
}];
于 2014-06-25T17:05:47.557 回答