Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the angle of the device?

Tags:

ios

I'm thinking that I just capture the starting UIAcceleration y value and compare it to the current value and then 'measure' but I don't exactly know if (1) that is right (2) what that math should be.

Thanks for any help.

Update:

Here is the code I used as suggested by jrturton.

#import "ViewController.h"

@interface ViewController()
{
  CMMotionManager *motionManager;
}
@end

@implementation ViewController
@synthesize angleLabel;

- (void)startMotion
{
  if (motionManager == nil)
  {
    motionManager = [[CMMotionManager alloc] init];
  }
  motionManager = [[CMMotionManager alloc] init];
  [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
    angleLabel.text = [NSString stringWithFormat:@"%g", motion.attitude.pitch];
  }];
}

- (void) stopMotion
{
  if (motionManager)
  {
    [motionManager stopDeviceMotionUpdates];
  }

  motionManager = nil;
}

- (void)viewDidUnload {
    [self setAngleLabel:nil];
    [super viewDidUnload];
}
@end
like image 817
cynicaljoy Avatar asked Dec 31 '11 21:12

cynicaljoy


1 Answers

It's easier than that. Use CMDeviceMotion's attitude property. See here for documentation.

like image 110
jrturton Avatar answered Sep 27 '22 19:09

jrturton