Accelerometer setup and usage

Tuesday, May 5, 2009

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

1@interface ApplicationViewController : UIViewController { ... } 

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

1- (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; } 

Other posts

Tagged with: