gpt4 book ai didi

ios - 如果 UIInterfaceOrientation 横向值相同,我应该如何确定横向 View 的方向?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:18:10 33 4
gpt4 key购买 nike

我想呈现一个响应方向变化的 View ,但由于各种原因无法使用 iOS 的内置自动旋转。在 viewDidLoad 中,我使用当前方向来确定 View 的初始布局:

_originalOrientation = [UIDevice currentDevice].orientation;

// if we were launched flat or unknown, use the status bar orientation as a guide
if (_originalOrientation == UIDeviceOrientationUnknown ||
_originalOrientation == UIDeviceOrientationFaceDown ||
_originalOrientation == UIDeviceOrientationFaceUp) {

_originalOrientation = UIDeviceOrientationFromInterfaceOrientation([UIApplication sharedApplication].statusBarOrientation);
}

正如您从我的评论中看到的那样,我需要处理 [UIDevice currentDevice].orientation 不是可用方向(即设备是平面的或方向未知)的情况。在这种情况下,我尝试使用 statusBarOrientation 来推断设备的方向(使用 UIViewControllerinterfaceOrientation 将是获取相同的信息):

UIDeviceOrientation UIDeviceOrientationFromInterfaceOrientation(UIInterfaceOrientation interface) {
// note: because UIInterfaceOrientationLandscapeLeft and UIInterfaceOrientationLandscapeRight have the same value, this conversion can't distinguish them

if (interface == UIInterfaceOrientationLandscapeLeft) {
return UIDeviceOrientationLandscapeLeft;

} else if (interface == UIInterfaceOrientationLandscapeRight) {
return UIDeviceOrientationLandscapeRight;

} else if (interface == UIInterfaceOrientationPortraitUpsideDown) {
return UIDeviceOrientationPortraitUpsideDown;

} else {
return UIDeviceOrientationPortrait;
}
}

但是我的逻辑无法准确辨别这两种景观情况,因为它们被定义为 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) {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};

有没有其他方法可以区分 UIInterfaceOrientationLandscapeLeftUIInterfaceOrientationLandscapeRight?除了使用状态栏方向之外,还有更好的方法来执行我的“回退”逻辑吗?

最佳答案

你写道:

However my logic can't accurately discern the two landscape cases because they are defined UIApplication.h to be the same constant value

我很确定他们不是!界面和设备方向的值被交换。参见 UIApplication.h:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};

界面方向 UIInterfaceOrientationLandscapeLeft 等于设备方向 UIDeviceOrientationLandscapeRight,反之亦然。但是:

  • UIInterfaceOrientationLandscapeLeft != UIInterfaceOrientationLandscapeRight
  • UIDeviceOrientationLandscapeLeft != UIDeviceOrientationLandscapeRight

试试这段代码,我想这会解决问题:

UIDeviceOrientation UIDeviceOrientationFromInterfaceOrientation(UIInterfaceOrientation interface) {
// interface orientation left is device orientation right
if (interface == UIInterfaceOrientationLandscapeLeft) {
return UIDeviceOrientationLandscapeRight;

// interface orientation right is device orientation left
} else if (interface == UIInterfaceOrientationLandscapeRight) {
return UIDeviceOrientationLandscapeLeft;

} else if (interface == UIInterfaceOrientationPortraitUpsideDown) {
return UIDeviceOrientationPortraitUpsideDown;

} else {
return UIDeviceOrientationPortrait;
}
}

顺便说一句,您还应该考虑为您的函数使用自己的前缀而不是 UI,否则可能很容易错误地认为 UIDeviceOrientationFromInterfaceOrientation 是一个 API 函数。

关于ios - 如果 UIInterfaceOrientation 横向值相同,我应该如何确定横向 View 的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15996087/

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