如何获取设备UIInterfaceOrientation

时间:2013-10-03 03:36:10

标签: ios uiinterfaceorientation

我想在UIInterfaceOrientation更改时捕获。我知道如何使用UIDeviceOrientation做到这一点,但我想要防止任何事情,除了横向左/右和纵向。

我正在使用UIDeviceOrientation,但每次我面朝上时,一切都会在我的应用上疯狂。

所以我想知道如何做这样的事情

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];


        CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;

        CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;

if (interfaceOrientation landscape) {
            if (orientation == UIDeviceOrientationLandscapeLeft)
            {

             }
             else if (orientation == UIDeviceOrientationLandscapeRight)
            {

             }
      }

if (interfaceOrientation Portrait) {


}

所以我只看风景或肖像。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {

}

这是一个C函数,而不是一个客观的C函数。

UIInterfaceOrientation是一个枚举。

另一种选择是:

if (interfaceOrientation & (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)) {

}

来自UIApplication.h标题:

// Note that UIInterfaceOrientationLandscapeLeft is equal to UIDeviceOrientationLandscapeRight (and vice versa).
// This is because rotating the device to the left requires rotating the content to the right.
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};

/* This exception is raised if supportedInterfaceOrientations returns 0, or if preferredInterfaceOrientationForPresentation
   returns an orientation that is not supported.
*/
UIKIT_EXTERN NSString *const UIApplicationInvalidInterfaceOrientationException NS_AVAILABLE_IOS(6_0);

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};