Shannon Appelcline

Subscribe to Shannon Appelcline: eMailAlertsEmail Alerts
Get Shannon Appelcline: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: iPhone Developer

iPhone Developer: Article

Book Excerpt: iPhone in Action

Using the iPhone's accelerometers

Together gravity and force measurement represent the most obvious things that you can do with the accelerometers, but by no means are they the only things. We suspect that using the accelerometers to measure three-dimensional gestures will be one of their best (and most frequent) uses as the iPhone platform matures.

The Accelerometer and Gestures
Three-dimensional gestures are one of the coolest results of having accelerometers inside your iPhone. Using them, your users can manipulate your iPhone programs without ever having to touch (or even look at) the screen.

To recognize a gesture you must do two things. First, you must accurately track the movements that make up a gesture. Second, make sure that in doing so you won't recognize a random movement that wasn't intended to be a gesture at all.

Technically, recognizing a gesture requires only the coding foundation that we've discussed thus far. However, we're going to show one example that puts that foundation into real-world use by create a method that recognizes a shake gesture.

The Shake
We've defining a shake as a rapid shaking back and forth of your iPhone, like you might shake dice in your hand before you threw them. As usual, Apple's Accelerometer Graph is a great tool to use to figure out what's going on. It shows a shake as primarily having these characteristics, presuming a program that's running in portrait mode:

  • Movement is primarily along the x-axis, though there is less movement along the y-axis, and even less along the z-axis.
  • There are at least three peaks of movement, with alternating positive and negative forces.
  • All peaks are at least +/-1g, with at least one peak being +/-2g for a relatively strong shake.

We can use the above requirements to define the minimum requirements for a shake. If we wanted to tighten them up, we'd probably require four or more peaks of movement, but for now, this will do for us. On the flipside, we might want to decrease the .g requirements so that users don't have to shake their iPhone quite as much (and, in fact, we will). We've detailed the code that will watch for a shake in Listing 7.

We've generally followed the logic of what we saw when viewing the accelerometer graph, though with increased sensitivity, as we noted. Our didShake: method registers a shake if it sees three or more movements of at least .75g, at least one of which is 1.25g, with movements going in opposite directions of each other.

Start off by removing gravity from the accelerometer data (#1), as you did in previous examples. This time don't worry about the quirk at the beginning of data collection, because it won't register as a shake.

The main work of the function is found in its latter half (#4), which is called whenever movement continues to occur. First check if the strongest movement is along the x-axis (#5). If so, register the movement if it's at least .75g and if it's in the opposite direction as the last x-axis move (#6). Do the latter check by seeing if the product of the last two moves on that axis is negative; if so, one must have been positive and the other negative.

If the strongest move was actually on the y-axis (#7), check for a sufficiently strong y-axis move that's in the opposite direction as the last y-axis move (#8). We could have written a more restrictive shake checker that only looked for x-axis movement, or we could have written a less restrictive checker that also looked for z-axis movement, but we opted for this middle ground.

As long as movement continues without a break of more than a quarter of a second, the shakecount continues to accrue, but when movement stops (#2), the program is now ready to see whether a shake actually occurred. Do this by seeing if your shake count equals or exceeds 3 and if the largest movement exceeded 1.25g. Afterward all of the variables are reset to check for the next shake.

By building this shake checker as a separate method, you made it so that it could easily be integrated into a list of checks made in the accelerometer:didAccelerate: method. Listing 8 shows off a simple usage of this that changes the color of the iPhone's screen every time a shake occurs (using a nextColor method which could do whatever you want).

We expect that the shake is going to be the most common 3D gesture programmed into the iPhone. With our code, you've already got it ready to go, though you may choose to change its sensitivity or to make it work in either one or three dimensions.

Other gestures such as a tilt, a fan, a circle, or a swat may be of use, depending on the specifics of your program, but we leave that up to your own ingenuity. For now, we've covered all of the main points of the accelerometers, from orientation to gravity to movement to gestures.

More Stories By Christopher Allen

Christopher Allen is one of the leaders of the iPhone developer community. He is the host of, which is the largest independent community of iPhone-based web developers, and manages its mailing list. He is also one of the founders of iPhoneDevCamp and oversees its Hackathon, and is co-author of iPhone in Action:Introduction to Web and SDK Development. Christopher is a longtime technologist, and is also a leader in social software and was one of the authors of TLS, the next-generation SSL protocol.

More Stories By Shannon Appelcline

Shannon Appelcline is a writer and technologist. He was a participant in Charles River Media's Massively Multiplayer Game Development 2. He has also been published by Chaosium Inc., Issaries Inc., Jones Publishing, Partizan Press, White Wolf Publishing, and Wizards of the Coast. In 2007 he wrote over 350,000 words for professional publication, including books for Mongoose Publishing and Moon Design Publications. He has also written fiction published by Green Knight Publishing and comic books published by Skotos Tech.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.