首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取设备UIInterfaceOrientation

如何获取设备UIInterfaceOrientation
EN

Stack Overflow用户
提问于 2013-10-03 11:36:10
回答 1查看 2.7K关注 0票数 0

我正在尝试捕获UIInterfaceOrientation何时发生变化。我知道如何使用UIDeviceOrientation,但我想要防止任何东西,除了风景,左/右和肖像。

我使用的是UIDeviceOrientation,但每次我把它放在上面时,我的应用程序上的一切都会变得疯狂。

所以我想知道怎么做这样的事情

代码语言:javascript
复制
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) {


}

所以我只看风景或者肖像。

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2015-01-10 13:11:02

代码语言:javascript
复制
if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {

}

它是一个C函数,不是一个目标C函数。

UIInterfaceOrientation是一个枚举。

另一种选择是:

代码语言:javascript
复制
if (interfaceOrientation & (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)) {

}

从UIApplication.h标头:

代码语言:javascript
复制
// 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),
};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19150291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档