游戏.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);
}
];
}
我的视图控制器.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