Skip to content

iPhone SDK – UIAccelerometer

April 21, 2010

I just received a small request for information on this topic, so here goes…

First, ensure that you set up the ViewController that is utilizing the UIAccelerometer as a UIAccelerometerDelegate:

@interface myViewController : UIViewController <UIAccelerometerDelegate> {

}

Within your viewDidLoad method, you must define a UIAccelerometer object, and specify the delegate and updateInterval properties:

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer]; —referencing our UIAccelerometer data for the application

accel.delegate = self; —set our current class as the delegate

accel.updateInterval = 1.0f/30.0f —check the accelerometer data 30x per second

Next, we must implement the – (void) accelerometer:(UIAccelerometer *)accel didAccelerate:(UIAcceleration *) acceler delegate method. This method is called the number of times you specify in the updateInterval property per second. The change in Accelerometer X, Y, and Z axis data can be accessed using acceler.x, acceler.y, acceler.z.

Usefull C-based function: fabsf([float val]) —a function that returns the absolutely value of a floating point number.

Advertisements

From → regular

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: