- Accessing Basic Device Information
- Adding Device Capability Restrictions
- Recipe: Checking Device Proximity and Battery States
- Recipe: Recovering Additional Device Information
- Recipe: Using Acceleration to Locate "Up"
- Working with Basic Orientation
- Retrieving the Current Accelerometer Angle Synchronously
- Recipe: Using Acceleration to Move Onscreen Objects
- Recipe: Accelerometer-Based Scroll View
- Recipe: Core Motion Basics
- Recipe: Retrieving and Using Device Attitude
- Detecting Shakes Using Motion Events
- Recipe: Using External Screens
- Tracking Users
- One More Thing: Checking for Available Disk Space
- Summary
Working with Basic Orientation
The UIDevice class uses the built-in orientation property to retrieve the physical orientation of the device. iOS devices support seven possible values for this property:
- UIDeviceOrientationUnknown—The orientation is currently unknown.
- UIDeviceOrientationPortrait—The home button is down.
- UIDeviceOrientationPortraitUpsideDown—The home button is up.
- UIDeviceOrientationLandscapeLeft—The home button is to the right.
- UIDeviceOrientationLandscapeRight—The home button is to the left.
- UIDeviceOrientationFaceUp—The screen is face up.
- UIDeviceOrientationFaceDown—The screen is face down.
The device can pass through any or all of these orientations during a typical application session. Although orientation is created in concert with the onboard accelerometer, these orientations are not tied in any way to a built-in angular value.
iOS offers two built-in macros to help determine if a device orientation enumerated value is portrait or landscape: namely UIDeviceOrientationIsPortrait() and UIDeviceOrientationIsLandscape(). It is convenient to extend the UIDevice class to offer these tests as built-in device properties:
@property (nonatomic, readonly) BOOL isLandscape; @property (nonatomic, readonly) BOOL isPortrait; - (BOOL) isLandscape { return UIDeviceOrientationIsLandscape(self.orientation); } - (BOOL) isPortrait { return UIDeviceOrientationIsPortrait(self.orientation); }
Your code can subscribe directly to device reorientation notifications. To accomplish this, send beginGeneratingDeviceOrientationNotifications to the currentDevice singleton. Then add an observer to catch the ensuing UIDeviceOrientationDidChangeNotification updates. As you would expect, you can finish listening by calling endGeneratingDeviceOrientationNotification.