0

我试图捕捉 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) {


}

所以我只看风景或肖像。

任何帮助将不胜感激。

4

1 回答 1

2
if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {

}

It's a C function, not an objective C function.

UIInterfaceOrientation is an enum.

The other option would be:

if (interfaceOrientation & (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)) {

}

From the UIApplication.h header:

// 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),
};
于 2015-01-10T05:11:02.367 回答