【问题标题】:UIOrientation returns 0 or 5UIOrientation 返回 0 或 5
【发布时间】:2010-07-25 00:12:57
【问题描述】:

我正在运行一个在多个区域中调用的简单函数,以帮助处理 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

【问题讨论】:

    标签: iphone objective-c ipad orientation uiinterfaceorientation


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 一旦我通过旋转设备改变方向,它总是正确的。但我不明白为什么它会是未知的或返回 FaceUp。如果当前未知,我需要调用一个函数让它找到方向吗?
    • 我假设当设备与地面平行且尚未旋转时返回 FaceUp。 (我稍微编辑了我的答案,因为显然我数不清。)
    • 我在查找了一些资料后再次编辑了我的答案。 (我承认我主要做桌面的东西。)
    • 谢谢 - 我相信你是对的。如果返回未知,我已经更新了我的代码以假设应用程序是纵向的。这似乎解决了我的困境。
    【解决方案2】:

    我会推荐使用UIDeviceOrientationIsValidInterfaceOrientation(orientation)

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

    这就是我的做法:

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

    【讨论】:

    • 谢谢鲍勃!我得试试看。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2014-03-31
      • 2023-03-26
      • 2011-05-04
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多