deviceMotion not returning anything for pitch, roll, or yaw

陌路散爱 提交于 2020-01-05 04:00:31

问题


I'm trying to get some simple code working to log the pitch, roll, and yaw of my iPhone 4S. It's firing the timer correctly, but the values of pitch, roll, and yaw, are always displayed in the console as 0.0000000. What am I missing here? Any help appreciated!

Here's my .h file:

@interface ViewController : UIViewController

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

-(void)doSomething;

@end

And here's my .m file:

#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:


You don't seem to be assigning anything to _motionManager. You need to create an instance of this class, otherwise you are just sending messages to nil.



来源:https://stackoverflow.com/questions/9605480/devicemotion-not-returning-anything-for-pitch-roll-or-yaw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!