concept accelerometer in category ios

appears as: accelerometer, accelerometers, n accelerometer, ccelerometers, The accelerometers
iOS in Practice

This is an excerpt from Manning's book iOS in Practice.

In this chapter, we’ll look at these concepts and explore how the user can access these ways of interacting and how to use them in an app. You’ll build a simple app that uses the Settings app for setting values. You’ll also use the audio framework to play a sound file for audio feedback. And you’ll use the accelerometer to detect shaking of the device and use that in your UI.

Let’s continue with this bare UI by using the accelerometer to detect shaking and handle that as part of your interface instead of buttons.

iOS 4 in Action: Developing iPhone and iPad Apps

This is an excerpt from Manning's book iOS 4 in Action: Developing iPhone and iPad Apps.

Generally speaking, a game application may combine the use of the accelerometer, drawing and animations, audio, and Game Kit. Once you’re familiar with the iOS SDK basics, you can jump to later chapters that will cover the iOS frameworks in detail. Chapter 15 provides complete coverage of game application development.

Figure 10.1. The accelerometers measure acceleration in three-dimensional space.

When the accelerometers are at rest, they naturally detect gravity. You can use this feature to detect the precise orientation an iPhone or iPad is currently held in, going far beyond the four or six states supported by the orientation variables.

Filtering and the accelerometer

The following code shows how you can use the accelerometers to modify redBall, a UIImage picture of a red ball initially set in the middle of the screen:

- (void)accelerometer:(UIAccelerometer *)accelerometer
    didAccelerate:(UIAcceleration *)acceleration {
    CGPoint curCenter = [redBall center];
    float newX = 3 * acceleration.x + curCenter.x;
    float newY = -3 * acceleration.y + curCenter.y;
    if (newX < 25) newX = 25;
    if (newY < 25) newY = 25;
    if (newX > 295) newX = 295;
    if (newY > 455) newY = 455;
    redBall.center = CGPointMake(newX,newY);
}

Any accelerometer program begins with the accelerometer:didAccelerate: method, which you access by setting the current program as a delegate of the Accelerometer shared action. You then mark the current position of the redBall.

To access the accelerometer, all you do is look at the x and y coordinates of the UIAcceleration object and prepare to modify the redBall’s position based on those. The acceleration is multiplied by 3 here to keep the ball’s movement from being snaillike. There’s also a z property for the third axis and a timestamp property indicating when the UIAcceleration object was created, none of which you need in this example. Movement has a limited effect on the example anyway, because an abrupt movement doesn’t change the ball’s slow roll much.

By building this shake checker as a separate method, you could easily integrate it into a list of checks made in the accelerometer:didAccelerate: method. The following code shows a simple use that changes the color of the screen every time a shake occurs. The nextColor method can be changed to do whatever you want:

iPhone and iPad in Action: Introduction to SDK Development

This is an excerpt from Manning's book iPhone and iPad in Action: Introduction to SDK Development.

Among these hardware features, those that stand out are the accelerometers and the GPS, which aren’t the sort of things commonly available to cell phone programmers. As you’ll see, they spotlight orientation awareness and location awareness. The iPad has been stripped of many of the bells and whistles that are on the iPhone. Many users were disappointed when they found out the iPad would be shipped without a camera. Of the hardware features in table 1.2, the iPad only has Bluetooth, accelerometers, and a compass.

When the accelerometers are at rest, they naturally detect gravity. You can use this feature to detect the precise orientation an iPhone or iPad is currently held in, going far beyond the four or six states supported by the orientation variables.

Filtering and the accelerometer

The following code shows how you can use the accelerometers to modify redBall, a UIImage picture of a red ball, created in Interface Builder and initially set in the middle of the screen:

- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration {
CGPoint curCenter = [redBall center];
float newX = 3 * acceleration.x + curCenter.x;
float newY = -3 * acceleration.y + curCenter.y;
if (newX < 25) newX = 25;
if (newY < 25) newY = 25;
if (newX > 295) newX = 295;
if (newY > 455) newY = 455;
redBall.center = CGPointMake(newX,newY);
}

Any accelerometer program begins with the accelerometer:didAccelerate: method, which you access by setting the current program as a delegate of the Accelerometer shared action. You then mark the current position of the redBall.

To access the accelerometer, all you do is look at the x and y coordinates of the UIAcceleration object and prepare to modify the redBall’s position based on those. The acceleration is multiplied by 3 here to keep the ball’s movement from being snaillike. There’s also a z property for the third axis and a timestamp property indicating when the UIAcceleration object was created, none of which you need in this example. Movement has a limited effect on the example anyway, because an abrupt movement doesn’t change the ball’s slow roll much.

If you want to write programs using acceleration gestures, we suggest that you download the Accelerometer Graph program available from Apple’s developer site. This is a nice, simple example of accelerometer use; but more important, it also provides you with a clear display of what the accelerometers report as you make different gestures. Make sure you enable the high-pass filter to get the clearest results.

Figure 10.3 shows what the Accelerometer Graph looks like in use (but without movement occurring). As you move the device around, you’ll quickly come to see how the accelerometers respond.

Figure 10.3. The Accelerometer Graph shows movement in all three directions.

Here are some details you’ll notice about how the accelerometers report information when you look at the Accelerometer Graph:

iPhone in Action: Introduction to Web and SDK Development

This is an excerpt from Manning's book iPhone in Action: Introduction to Web and SDK Development.

Of these hardware features, the ones that really stand out are the accelerometers and the GPS, which are not the sort of things commonly available to cell phone programmers. As you’ll see, they spotlight two of the elements that make the iPhone unique: orientation awareness and location awareness. However, before we fully explore the iPhone’s unique features, it’s useful to put the device in perspective by comparing the iPhone to the mobile state of the art.

When the accelerometers are at rest, they naturally detect gravity. This may be used to detect the precise orientation that an iPhone is currently held in, going far beyond the four or six states supported by the orientation variables.

Listing 17.4. A low-pass filter isolates gravity in the accelerometers