O'Reilly logo

Beginning iOS 4 Application Development by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 15. Accessing the Accelerometer

WHAT YOU WILL LEARN IN THIS CHAPTER

  • How to obtain accelerometer data from your iPhone

  • How to detect shakes to your device

iPhone's built-in accelerometer enables your program to detect the orientation of the device and adapt the content to suit the new orientation. For example, when you rotate your device sideways, the Safari Web browser automatically switches the screen to landscape mode to provide a wider viewing space.

In this chapter, you learn how to access the accelerometer and use the Shake API to detect shakes to your iPhone.

Using the Accelerometer

The accelerometer in iPhone measures the acceleration of the device relative to freefall. A value of 1 indicates that the device is experiencing 1 g of force exerted on it (1 g of force being the gravitational pull of the earth, which your device experiences when it is stationary). The accelerometer measures the acceleration of the device in three different axes: x, y and z. Figure 15-1 shows the different axes measured by the accelerometer.

Figure 15-1

Figure 15.1. Figure 15-1

Table 15-1 shows example readings of the three axes when the device is in the various positions. Bear in mind that you won't see the exact same values as these, because they are always fluctuating due to the accelerometer's sensitivity.

Table 15.1. Example Readings of the X, Y, and Z Axes

POSITION

X

Y

Z

Vertical upright position

0.0

−1.0

0.0

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required