|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
TOP THREE LINKS YOU MUST CLICK ON Features Book Excerpt: iPhone in Action
Using the iPhone's accelerometers
Nov. 5, 2008 11:00 AM
The iPhone's accelerometers can provide access to a variety of information about where an iPhone exists in space. By measuring gravity, you can easily discover an iPhone's precise orientation. By measuring movement, you can see how an iPhone is being guided through space. Further, you can build more complex movements into three-dimensional gestures, such as the shake. Although we usually think about the iPhone's touch screen in reference to input, the accelerometers provide another method that could allow users to make simple adjustments to a program. We can imagine game controls and painting programs both built entirely around the accelerometers. The Accelerometer and Orientation There are two ways to access the UIDevice information: through properties and through a notification. The Orientation Property UIDevice *thisDevice = [UIDevice currentDevice]; Once you've done this, look at the orientation property. It will return a constant drawn from UIDeviceOrientation. This looks exactly like the results you can get from a view controller's orientation property except there are two additional values, as shown in Table 1.
Table 1: UIDeviceOrientation lists six values for a device's orientation These additional values show off the first reason that you might want to access the UIDevice object rather than examining orientation using a view controller. The Orientation Notification As shown, this is a two-step process. First alert the iPhone that you're ready to start listening for notification that the iPhone has changed its orientation (#1). This is one of a pair of UIDevice methods, the other of which is endGeneratingDeviceOrientationNotifactions. Generally you should only leave notifications on when you actually need them, as they take up CPU cycles and thus increase your power consumption. Second, register to receive the UIDeviceOrientationDidChangeNotification messages (#2), our first real-live example of the notification methods. Thereafter, whenever an orientation change notification occurs, your deviceDidRotate: method will be called. Note that you won't actually receive notification of what the new orientation is, simply that the change happened. For more, you'll have to go out and query the orientation property, as normal. We've now seen the two ways in which an iPhone's orientation can be tracked with the UIDevice object, providing more information and more rapid notification than we received when using the view controller. However, that only touches the surface of what you can do with the iPhone's accelerometers. It's probably the raw data about changes in three-dimensional space that you really want to access. WIRELESS BUSINESS & TECHNOLOGY LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||