4

我正在运行一个在多个区域调用的简单函数,以帮助在方向更改期间处理 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 方向的权威文章:http: //www.donttrustthisguy.com/orientating-yourself-in-ios

4

3 回答 3

8

您是否查看了 的枚举值UIInterfaceOrientation?从文档:

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

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

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

于 2010-07-25T00:19:39.433 回答
3

我建议使用UIDeviceOrientationIsValidInterfaceOrientation(orientation)

它会告诉您它是否是有效的方向(有效的是横向或纵向,而不是 FaceUp/FaceDown/UnKnown)。然后你可以把它当作它的肖像,如果它是未知的。

我就是这样做的:

if (UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
    // handle landscape
} else {
    // handle portrait
}
于 2012-05-15T22:37:03.830 回答
0

[UIDevice currentDevice].orientation返回一个UIDeviceOrientation

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

您的getWidthAndHeightForOrientation函数需要一个UIInterfaceOrientation参数:

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

这些类型虽然相关,但不是一回事。您可以使用 . 从任何视图控制器访问当前界面方向self.interfaceOrientationUIInterfaceOrientation有 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
};
于 2013-08-06T16:03:50.977 回答