2012-07-30 2 views
0

이제 코드가 작동하지만 여전히 작업이 필요합니다. 내가 얻는 가치는 "끈적"하고, 안정적이지 않다. (자기 북쪽으로 되돌아 가려고 할 때마다 조금 움직이는 것 같다.) 나는 그 장치를 조금씩 흔드는 것이 필요하다. 값 ..이 코드를 사용하여 자기 북쪽에서 장치의 편차를 읽을 수없는 이유는 무엇입니까?

Game.h

#import <Foundation/Foundation.h> 

#import "CoreLocation.h" 

@interface Game : NSObject 

<CLLocationManagerDelegate> 

@property BOOL stopButtonPressed; 

-(void) play; 

@end 

Game.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 

MyViewController.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 

내가 뭘 잘못하고 있니? https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/occ/intf/CLLocationManagerDelegate

답변

2

이 코드는 스레드를 차단하고 주 스레드에서 일어나고있는 경우에는 버튼을 눌러 얻을하지 않습니다 :

+0

은 '아무튼 메인 스레드를 막고있는 것처럼 보입니다. 콘솔에서 "제목 사용 가능"을 얻은 다음 "학위 : 0.000"을 계속 가져옵니다. –

+0

무한 루프에서 오른쪽으로 이동합니다. 고정되어 있지 않습니다. –

+0

나는 '(game performSelectorInBackground : @selector (play) withObject : nil];' –

0

는 대신 직접 속성을 호출하는 CLLocationManager 대리자 메서드를 잡을 것이다.

CLLocationManager는 비동기 메커니즘입니다. 제대로 작동하려면이 위치 업데이트를 사용할 때 통지합니다 대리자를 제공해야합니다 (이 self가의 ViewController 또는 유사한)는 대부분의 경우 self (수 있습니다. 참조 CLLocationManagerDelegate docs

... 
    CLLocationManager *locationManager; 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    [locationManager startUpdatingHeading]; 
} 

- (void)locationManager:manager didUpdateHeading:newHeading { 
    double degrees = newHeading.magneticHeading; 
    NSLog(@"Degrees : %F", degrees); 
}