2011-03-16 26 views
0

我最近在做指南針,涉及CoreMotion框架,但是我查了很久沒有找到關於如何實現這個框架的指南針效果的信息,希望能夠得到大家的幫助。CoreMotion框架

回答

0

我的 「HelloWorld」 CoreMotion:

1)在.h文件中添加CoreMotion.framework

2):

#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)]; 
}];