2012-03-07 12 views
1

私のiPhone 4Sのピッチ、ロール、ヨーを記録する簡単なコードを取得しようとしています。タイマーは正しく作動しますが、ピッチ、ロール、ヨーの値は常にコンソールに0.0000000として表示されます。私はここで何が欠けていますか?どんな助けにも感謝!deviceMotionがピッチ、ロール、またはヨーに対して何も返さない

@interface ViewController : UIViewController 

@property (nonatomic, retain) CMMotionManager *motionManager; 
@property(readonly, weak) CMDeviceMotion *deviceMotion; 

-(void)doSomething; 

@end 

そして、ここに私の.mファイルです:あなたは_motionManagerに何かを割り当てることしていないようです

#import "ViewController.h" 

@implementation ViewController 

@synthesize motionManager = _motionManager; 
@synthesize deviceMotion = _deviceMotion; 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [_motionManager startDeviceMotionUpdates]; 
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(doSomething) userInfo:nil repeats:YES]; 
} 

- (void)doSomething 
{ 
    NSLog(@"Reading Device Motion Updates.."); 
    _deviceMotion = [_motionManager deviceMotion]; 
    NSLog(@"Roll = %f, Pitch = %f, Yaw = %f", _deviceMotion.attitude.roll, _deviceMotion.attitude.pitch, _deviceMotion.attitude.yaw); 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    [_motionManager stopDeviceMotionUpdates]; 
} 

@end 

答えて

1

は、ここに私の.hファイルです。このクラスのインスタンスを作成する必要があります。それ以外の場合は、メッセージをnilに送信するだけです。

+0

私はそれを.hファイルで作成しています。 @property(非構造、保持)CMMotionManager * motionManager; viewDidLoadで開始するように指示します。 [_motionManager startDeviceMotionUpdates]; これでは不十分ですか? – Luke

+0

ああ、私はそれをalloc'dしていなかった。今すぐソートされました。 – Luke

+0

ヘッダーファイルには作成していません。 '@ property'は、そのプロパティを通してメンバ変数にアクセスできることを宣言します。 '@ synthesize'はメンバ変数へのアクセスを提供するメソッドを生成します。しかし、実際には、インスタンス変数/プロパティにオブジェクトを作成して割り当てる必要があります。それ以外の場合はアクセスすることはありません。 – Jim

関連する問題