deviceMotion没有为俯仰,翻滚或偏航返回任何东西

时间:2012-03-07 16:35:16

标签: objective-c cmmotionmanager

我正在尝试使用一些简单的代码来记录我的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

1 个答案:

答案 0 :(得分:1)

您似乎没有向_motionManager分配任何内容。您需要创建此类的实例,否则您只是向nil发送消息。