2

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
4

3 に答える 3

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

乾杯、別名

于 2012-07-31T00:31:16.640 に答える
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);
                                             }
     ];
}

問題は、CMMotionManager作成しているものがメソッドの最後で割り当て解除されていることです。これは、playメソッドを保持しているものがないためです。したがって、モーションマネージャがなくなったために、ハンドラがコールバックされることはありません。

于 2012-07-31T21:29:35.500 に答える
0

ところで、1つの可能性は、実行しているデバイスに磁力計さえない可能性があることです。磁力計付きのアップルデバイス を指すmagnetometer-for-compass-on-ipod-touch-4gを参照してください。

于 2012-07-31T12:28:18.707 に答える