为什么我不用这个简单的代码读取任何磁场值?

时间:2012-07-30 22:49:58

标签: iphone objective-c ios magnetometer

Game.m

#import "Game.h"
#import "CoreMotion.h"

@implementation Game

- (id) init 
{
    self = [super init];

    self.stopButtonPressed = NO;

    return self;
}
-(void) play
{
     CMMotionManager *motionManager;
     motionManager = [[CMMotionManager alloc] init];
     motionManager.deviceMotionUpdateInterval = 1.f/10.f;
     [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                        withHandler:^(CMDeviceMotion *motion, NSError *error)
                                        {
                                          NSLog(@"--------------> %i %i", motionManager.deviceMotionActive , motionManager.deviceMotionAvailable);

                                          NSLog(@"Degrees : %F",atan(motion.magneticField.field.y/fabs(motion.magneticField.field.x)) * 180.0 / M_PI);
                                        }
     ];
}

MyViewController.m

#import "MyViewController.h"
#import "Game.h"

@interface MyViewController()
{
    Game *game;
}
@end

@implementation MyViewController

-(void) viewDidLoad
{
    game = [[Game alloc] init];
}

- (IBAction)playPressed:(UIButton *)sender 
{
    // using a separate thread
    //[game performSelectorInBackground:@selector(play) withObject:nil];

    // not using a separate thread
    [game play] ;
}

- (IBAction)stopPressed:(UIButton *)sender 
{
    game.stopButtonPressed = YES;
}

@end

3 个答案:

答案 0 :(得分:1)

调用方法startDeviceMotionUpdates后,磁场值无法立即显示。您需要尝试稍后检索该值(即使用NSTimer并检查更新。

虽然这应该有用,但这不是一个好习惯。如果您只需要磁场值,则应查看CMMotionManager的文档,并使用方法startMagnetometerUpdatesToQueue:withHandler:,如下所示:

[motionManager startMagnetometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
  CMMagneticField field = magnetometerData.magneticField;
  NSLog(@"x: %f  y:%f  z:%f", field.x, field.y, field.z);
}];

欢呼声, ANKA

答案 1 :(得分:1)

您需要抓住CMMotionManager。所以为它创建一个实例变量。例如:

Game.m

#import "Game.h"
#import "CoreMotion.h"

@interface Game ()
@property (nonatomic, strong) CMMotionManager *motionManager;
@end

@implementation Game

@synthesize motionManager = _motionManager;

- (id) init 
{
    self = [super init];

    self.stopButtonPressed = NO;

    return self;
}
-(void) play
{
     self.motionManager = [[CMMotionManager alloc] init];
     self.motionManager.deviceMotionUpdateInterval = 1.f/10.f;
     [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                             withHandler:^(CMDeviceMotion *motion, NSError *error)
                                             {
         NSLog(@"--------------> %i %i", self.motionManager.deviceMotionActive , self.motionManager.deviceMotionAvailable);
         NSLog(@"Degrees : %F",atan(motion.magneticField.field.y/fabs(motion.magneticField.field.x)) * 180.0 / M_PI);
                                             }
     ];
}

问题是,您正在创建的CMMotionManagerplay方法结束时被释放,因为没有任何内容。所以处理程序永远不会被回调,因为你的运动经理已经离开了。

答案 2 :(得分:0)

BTW一种可能性是您运行的设备甚至可能没有磁力计。请参阅magnetometer-for-compass-on-ipod-touch-4g我指向apple-devices-with-magnetometer

的位置