Writing an Accelerometer Application
Let’s go ahead and implement a simple application to illustrate how to approach the accelerometer. Open Xcode and start a new View-based application for the iPhone, and name the project “Accelerometer” when prompted for a filename.
Warning
The raw accelerometer data can also be accessed using the Core
Motion framework, which was new in iOS 4.0. I talk about how to do this
in Chapter 6. It is
therefore possible, even likely, that the UIAccelerometer class discussed in this
chapter my be deprecated in a future iOS release.
Click on the AccelerometerViewController.xib file to open it
into Interface Builder. Since you want to both report the raw figures from
the accelerometer and also display them using a progress bar, go ahead and
drag and drop three UIProgressView
controls from the Object Library into the View window. Then add two
UILabel elements for each progress bar:
one to hold the X, Y, or Z label and the other to hold the accelerometer
measurements. After you do that, the view should look something a lot like
Figure 4-2.

Figure 4-2. The Accelerometer application UI
Go ahead and close the Utilities panel and click to open the
Assistant Editor. Then Control-Click and drag from the three UIProgressView elements, and the three UILabel elements to the AcclerometerViewController.h header file. The header file should be displayed in the Assistant Editor ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access