Accelerometer setup and usage

To use the accelerometer in an iPhone application you need to add the <UIAccelerometerDelegate> into the .h file of your application:

@interface ApplicationViewController : UIViewController {

Then in your .m file you must setup the accelerometer:

- (void)viewDidLoad {
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1/30;

Update interval is in seconds so 1/30 is a 30th of a second. Now the accelerometer is setup you can use it:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

xValue = acceleration.x;
yValue = acceleration.y;
zValue = acceleration.z;
Published on

Filled under


comments powered by Disqus