UIOrientation返回0或5

Jim*_*ers 4 iphone objective-c orientation ipad uiinterfaceorientation

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

- (void) getWidthAndHeightForOrientation:(UIInterfaceOrientation)orientation {
    NSLog(@"New Orientation: %d",orientation);
end
Run Code Online (Sandbox Code Playgroud)

我在各种各样的地方称它为:

[self getWidthAndHeightForOrientation: [[UIDevice currentDevice] orientation]];
Run Code Online (Sandbox Code Playgroud)

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

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

更新:

因为处理方向的方式我在代码中不断发现错误,所以我写了一篇关于如何处理UIDevice或UIInterface方向的权威帖子:http://www.donttrustthisguy.com/orientating-yourself-in-ios

Wev*_*vah 8

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

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;
Run Code Online (Sandbox Code Playgroud)

因此可以想象它可以是0-6之间的任何东西.

编辑:也许你应该使用这些方法对你UIViewController(willRotateToInterfaceOrientation:duration:等)而不是调用orientationUIDevice