gpt4 book ai didi

ios - 如何获取设备 UIInterfaceOrientation

转载 作者:行者123 更新时间:2023-11-28 22:25:22 27 4
gpt4 key购买 nike

我正在尝试捕捉 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) {


}

所以我只看风景或肖像。

如有任何帮助,我们将不胜感激。

最佳答案

if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {

}

这是一个 C 函数,而不是一个 objective-c 函数。

UIInterfaceOrientation 是一个枚举。

另一种选择是:

if (interfaceOrientation & (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)) {

}

来自 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),
};

关于ios - 如何获取设备 UIInterfaceOrientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19150291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com