【问题标题】:shouldAutorotate behavior in iOS 8iOS 8 中的 shouldAutorotate 行为
【发布时间】:2014-10-22 08:39:55
【问题描述】:

我发现 UIViewController shouldAutorotate 方法在 7.1 和 8 之间有一个小的行为变化。 Apple View Controller Programming Guide 声明在执行任何自动旋转之前调用此方法

但是我注意到,当我简单地禁用 shouldAutorotate(返回 NO)时,该方法会在纵向 -> 横向旋转上调用,但不会在以下横向 -> 纵向旋转上调用。同样,该方法应始终按照我的理解进行调用。在 iOS 8 上运行时会出现这种情况,而在 iOS 7.1 上则不会。

这似乎与 iOS8 调用堆栈中的一个新方法有关:

[UIWindow shouldAutorotateToInterfaceOrientation:checkForDismissal:isRotationDisabled]

我找不到关于此方法及其行为的任何描述,知道在哪里可以找到有关此内部方法的更多信息

重现此的简单步骤:

  1. 在 Xcode 中创建一个新的 Single View Application 项目
  2. 更新您的 ViewController.m 以实现 shouldAutorotate

    - (BOOL)shouldAutorotate
    {
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
        NSLog(@"%s orientation is %@", __PRETTY_FUNCTION__, [self stringFromDeviceOrientation:orientation]);
        // Disable autorotation of the interface.
        return NO;
    }
    
    - (NSString *) stringFromDeviceOrientation:(UIDeviceOrientation)uido
    {
        NSString *orientation = @"UIDeviceOrientationUnknown";
        switch (uido) {
            case UIDeviceOrientationPortrait:
                orientation = @"Portrait";
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                orientation = @"PortraitUpsideDown";
                break;
            case UIDeviceOrientationLandscapeRight:
                orientation = @"LandscapeRight";
                break;
            case UIDeviceOrientationLandscapeLeft:
                orientation = @"LandscapeLeft";
                break;
            case UIDeviceOrientationFaceDown:
                orientation = @"FaceDown";
                break;
            case UIDeviceOrientationFaceUp:
                orientation = @"FaceUp";
                break; 
                default:
                    break;
            }
            return orientation;
        }
    
  3. 在模拟器 iPhone 5s (7.1) 上运行:shouldAutorotate 在切换到横向并返回到纵向时被调用

  4. 在模拟器 iPhone 5s (8.0) 或 iPhone 6 上运行:shouldAutorotate 在切换到横向时被调用,但在切换回纵向时不被调用

【问题讨论】:

    标签: ios objective-c iphone


    【解决方案1】:

    同样的事情也发生在我身上。我感觉 iOS8 中存在错误,因为我的相同代码在 iOS7 中运行良好。同样,作为用户,我在使用的应用程序中遇到了很多旋转问题。在 8 个shouldAutorotate 调用中,当您旋转到一侧时,但当您返回正常旋转时不再调用。除非它实际上是自动旋转到一边,在这种情况下,它会在返回时被调用。

    这是因为您的“shouldAutorotate”总是返回“NO”。我的假设是,出于某种原因,在 iOS8 中,他们(改变这种行为的苹果程序员)决定,如果他们在旋转到侧面时得到 NO,则在旋转回纵向时不需要调用 shouldAutorotate。所以它破坏了我们预期的行为。

    没有人谈论它或抱怨它。对于这种确切情况,Google 几乎没有返回任何结果。我认为这是因为为了实现这一点,您必须尝试使用​​“shouldAutorotate”作为设备轮换通知,但实际上并未使用操作系统的自动轮换功能。比如我做opengl游戏。所以我只是让我的引擎知道旋转游戏图形。但我没有旋转我的观点。我认为没有多少人将它用于设备轮换通知。

    所以我决定改用设备通知。在我的视图控制器中:

    - (void) movingToBackground: (NSNotification *) notification {
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    }
    
    - (void) movingToForeground: (NSNotification *) notification {
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    }
    

    并接收消息:

       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewChanging:) name:UIDeviceOrientationDidChangeNotification object:nil];
    

    以及方法:

    - (void)viewChanging:(NSNotification*)notification
    {
       NSLog(@"View is changing");
       previousOrientation = orientation;
       orientation = [[UIDevice currentDevice] orientation];
    
       if (previousOrientation==orientation && forceReorientation==NO) {
           return;
       }
       ... do stuff ...
    

    然后回到我的视图控制器,我将所有自动旋转的东西都设置为 NO。

    【讨论】:

    • 我同意你的分析,返回途中的方法没有被调用可能是由于这个新的shouldAutorotateToInterfaceOrientation 及其isRotationDisabled 标志。您的解决方法(使用通知)是正确的方法,并且不能保证会调用自动旋转代码,尤其是现在 iOs 8 会随着大小的变化处理旋转。我会在接受您的回答之前稍等片刻,以防有更多信息出现。
    • 谢谢。顺便说一句,我现在在我的应用程序中使用上面的代码,它似乎正在工作。我唯一担心的是 beginGeneratingDeviceOrientationNotifications 可能会在每次轻微旋转时导致太多通知,即使它不是完整的 90 度。但除了实际设备旋转外,我没有看到“视图正在改变”打印。所以我觉得很好。
    • 你有解决办法吗?我对状态栏进行了精彩的旋转控制。还实现了从另一个应用程序或从另一个方向返回的加速度计协调的 url 协议导航。但是,可悲的是,在屏幕键盘应相应旋转的步骤上停止了。在我的解决方案中,我(和你一样)有一个固定的纵向视图及其 OpenGL 层。如何强制 iOS 不为其可怕的旋转设置动画??? :-)
    • 我不完全确定我是否遵循。但是键盘解决方案是在他们从键盘输入输入时强制 UI 是直立的。强迫用户为某些事情旋转手机是完全可以的。在 OpenGL 游戏中,他们不会经常使用键盘。
    猜你喜欢
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2013-12-05
    • 1970-01-01
    相关资源
    最近更新 更多