好的,代码现在可以工作了,但它仍然需要工作。我得到的值是“粘性的”,它们不稳定(每次我尝试返回时,磁北似乎都会移动一点),我需要稍微摇晃设备以刷新/唤醒价值观..
游戏.h
#import <Foundation/Foundation.h>
#import "CoreLocation.h"
@interface Game : NSObject
<CLLocationManagerDelegate>
@property BOOL stopButtonPressed;
-(void) play;
@end
游戏.m
@implementation Game
- (id) init
{
self = [super init];
self.stopButtonPressed = NO;
CLLocationManager *locationManager;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
return self;
}
-(void) play
{
[locationManager startUpdatingHeading];
while(!self.stopButtonPressed)
{
double degrees = locationManager.heading.magneticHeading;
int degreesRounded = (int)degrees;
NSLog(@"Degrees : %i", degreesRounded);
}
}
@end
我的视图控制器.m
@interface MyViewController()
{
Game *game;
}
@end
@implementation MyViewController
-(void) viewDidLoad
{
game = [[Game alloc] init];
}
- (IBAction)playPressed:(UIButton *)sender
{
[game performSelectorInBackground:@selector(play) withObject:nil];
}
- (IBAction)stopPressed:(UIButton *)sender
{
game.stopButtonPressed = YES;
}
@end
我究竟做错了什么?