UIOrientation 返回 0 或 5

发布于 2024-09-10 22:57:54 字数 814 浏览 4 评论 0原文

我正在运行一个简单的函数,该函数在多个区域中被调用,以帮助处理方向更改期间 iPad 应用程序上的布局。它看起来像这样:

- (void) getWidthAndHeightForOrientation:(UIInterfaceOrientation)orientation {
    NSLog(@"New Orientation: %d",orientation);
end

我在不同的地方这样称呼它:

[self getWidthAndHeightForOrientation: [[UIDevice currentDevice] orientation]];

该函数通常有一些简单的代码,如果方向是纵向或横向,则运行这些代码。不幸的是,当应用程序在位置 1 启动时,它没有按预期工作。结果我得到 0。稍后,如果以相同方式调用该函数但设备从未旋转过,我将返回值 5。这是什么意思?为什么它会抛出这些值?

简而言之,为什么 [[UIDevice currentDevice]orientation] 会抛出 0 或 5,而不是 1 到 4 之间的任何值?

更新:

因为我不断地在代码中发现由于处理方向的方式而导致的错误,所以我写了一篇关于如何处理 UIDevice 或 UIInterface 方向的明确文章: donttrustthisguy.com/orientating-yourself-in-ios" rel="nofollow noreferrer">http://www.donttrustthisguy.com/orientating-yourself-in-ios

I am running a simple function that get's called in multiple areas to help deal with layout on an iPad app during orientation changes. It looks like this:

- (void) getWidthAndHeightForOrientation:(UIInterfaceOrientation)orientation {
    NSLog(@"New Orientation: %d",orientation);
end

And I call it in various places like this:

[self getWidthAndHeightForOrientation: [[UIDevice currentDevice] orientation]];

The function normally has some simple code that runs if the orientation is portrait or landscape. Unfortunately it wasn't working as expected when the app is started in what would be position 1. I get 0 as a result. Later if the function is called in the same manner but the device has never been rotated I get back a value of 5. What does this mean? Why would it throw these values?

In short why would [[UIDevice currentDevice] orientation] ever throw 0 or 5 instead of any value between 1 and 4?

UPDATE:

Because I kept finding bugs in my code due to the way orientation was handled, I wrote a definitive post on how to handle UIDevice or UIInterface orientations: http://www.donttrustthisguy.com/orientating-yourself-in-ios

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雾里花 2024-09-17 22:57:54

您是否查看过 UIInterfaceOrientation 的枚举值?来自文档:

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

所以可以想象它可以是 0-6 之间的任何值。

编辑:也许您应该使用 UIViewController 上的方法(willRotateToInterfaceOrientation:duration: 等),而不是在 上调用 orientation >UIDevice

Did you take a look at the enum values for UIInterfaceOrientation? From the docs:

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

So it could conceivably be anything from 0-6.

Edit: Maybe you should be using the methods on your UIViewController (willRotateToInterfaceOrientation:duration:, etc.) instead of calling orientation on the UIDevice?

━╋う一瞬間旳綻放 2024-09-17 22:57:54

我建议使用 UIDeviceOrientationIsValidInterfaceOrientation(orientation)

它会告诉您其方向是否有效(横向或纵向有效,而不是 FaceUp/FaceDown/UnKnown)。那么,如果它不为人所知,你就可以把它当作它的肖像。

我就是这样做的:

if (UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
    // handle landscape
} else {
    // handle portrait
}

I would recommend using UIDeviceOrientationIsValidInterfaceOrientation(orientation)

It will tell you if its a valid orientation (valid being either landscape or portrait, not FaceUp/FaceDown/UnKnown). Then you can treat it as if its portrait if its unknown.

This is how I do it:

if (UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
    // handle landscape
} else {
    // handle portrait
}
南街女流氓 2024-09-17 22:57:54

[UIDevice currentDevice].orientation 返回一个 UIDeviceOrientation

该属性的值是一个常量,表示当前
设备的方向。该值代表物理量
设备的方向,可能与当前的方向不同
应用程序用户界面的方向。

您的 getWidthAndHeightForOrientation 函数采用 UIInterfaceOrientation 参数:

应用程序用户界面的方向。

这些类型虽然相关,但并不是同一件事。您可以使用 self.interfaceOrientation 从任何视图控制器访问当前的界面方向UIInterfaceOrientation 有 4 个可能的值,而 UIDeviceOrientation 有 9 个:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};

// 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
};

[UIDevice currentDevice].orientation returns a UIDeviceOrientation:

The value of the property is a constant that indicates the current
orientation of the device. This value represents the physical
orientation of the device and may be different from the current
orientation of your application’s user interface.

Your getWidthAndHeightForOrientation function takes a UIInterfaceOrientation parameter:

The orientation of the application's user interface.

These types, though related, are not the same thing. You can access the current interface orientation from any view controller using self.interfaceOrientation. UIInterfaceOrientation has 4 possible values while UIDeviceOrientation has 9:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};

// 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
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文