UIOrientation 返回 0 或 5
我正在运行一个简单的函数,该函数在多个区域中被调用,以帮助处理方向更改期间 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否查看过 UIInterfaceOrientation 的枚举值?来自文档:
所以可以想象它可以是 0-6 之间的任何值。
编辑:也许您应该使用
UIViewController
上的方法(willRotateToInterfaceOrientation:duration:
等),而不是在上调用
?orientation
>UIDeviceDid you take a look at the enum values for
UIInterfaceOrientation
? From the docs: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 callingorientation
on theUIDevice
?我建议使用 UIDeviceOrientationIsValidInterfaceOrientation(orientation)
它会告诉您其方向是否有效(横向或纵向有效,而不是 FaceUp/FaceDown/UnKnown)。那么,如果它不为人所知,你就可以把它当作它的肖像。
我就是这样做的:
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:
[UIDevice currentDevice].orientation
返回一个UIDeviceOrientation
:您的 getWidthAndHeightForOrientation 函数采用 UIInterfaceOrientation 参数:
这些类型虽然相关,但并不是同一件事。您可以使用
self.interfaceOrientation
从任何视图控制器访问当前的界面方向。UIInterfaceOrientation
有 4 个可能的值,而UIDeviceOrientation
有 9 个:[UIDevice currentDevice].orientation
returns aUIDeviceOrientation
:Your
getWidthAndHeightForOrientation
function takes aUIInterfaceOrientation
parameter: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 whileUIDeviceOrientation
has 9: