1

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

1 回答 1

1

你似乎没有分配任何东西给_motionManager. 您需要创建此类的一个实例,否则您只是将消息发送到nil.

于 2012-03-07T16:45:27.733 回答