Domanda

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
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top