我正试图获得一些简单的代码来记录我的iPhone 4S的俯仰、滚转和偏航。它正确地启动了计时器,但俯仰、滚转和偏航的值在控制台中始终显示为0.0000000。我在这里错过了什么?感谢您的帮助!
这是我的.h文件:
@interface ViewController : UIViewController
@property (nonatomic, retain) CMMotionManager *motionManager;
@property(readonly, weak) CMDeviceMotion *deviceMotion;
-(void)doSomething;
@end
这是我的.m文件:
#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
您似乎没有为_motionManager
分配任何内容。您需要创建这个类的一个实例,否则您只是在向nil
发送消息。