Orientation Changes
The iPhone is retrofitted with hardware to sense its state in the surrounding environment. One sensor in particular, the accelerometer, is able to determine the orientation that the iPhone is being held at. How to read the orientation and what to do with it when it's changed are important for applications that need to provide landscape mode support.
Reading the Orientation
The orientation of the iPhone can be read using a static method named deviceOrientation
, found in the UIHardware
class:
int orientation = [ UIHardware deviceOrientation: YES ];
This method returns one of six different possible orientations identifying how the iPhone is presently being held.
Orientation | Description |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The sensor can be read when the application first starts up, but what's more useful is to know when the orientation has been changed. A change in the orientation is reported automatically to the UIApplication
class, the class your GUI application is derived from. A method named deviceOrientationChanged
can be overridden ...
Get iPhone Open Application Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.