应该在iOS 8中执行操作

我在UIViewController shouldAutorotate方法中发现了7.1和8之间的一个小的行为改变。 Apple View Controller编程指南指出, 在执行任何自转之前调用此方法

但是,我注意到,当我简单地禁用shouldAutorotate(返回NO),方法调用肖像 – >风景旋转,但不是在以下景观 – >人像旋转。 应该总是按照我的理解来调用这个方法。 这发生在iOS 8上运行,而不是iOS 7.1上。

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

[UIWindow shouldAutorotateToInterfaceOrientation:checkForDismissal:isRotationDisabled]

我找不到这个方法和它的行为是否有任何描述,我可以find关于这个内部方法的更多信息的任何想法

简单的步骤来重现这一点:

  1. 在Xcode中创build一个新的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 ,但在切换回纵向时不调用它

同样的事情发生在我身上。 我有一种感觉,在iOS8中有一个错误,因为我相同的代码在iOS7中工作正常。 另外就像用户一样,我在使用的应用程序中遇到了很多旋转问题。 在旋转到侧面时应该调用8次shouldAutorotate调用,但当您返回到正常旋转时不会再次调用。 这是除非它实际上做了自转,在这种情况下返回它被称为。

所以这是因为你的'shouldAutorotate'总是返回'NO'。 我的假设是,由于iOS8的某些原因,他们(苹果编码人员改变了这种行为)决定,如果他们在旋转到侧面时得到NO,他们不需要在旋转回肖像时调用shouldAutorotate。 所以它打破了我们期待的行为。

没有人在谈论或抱怨。 谷歌几乎没有结果这个确切的情况。 我认为这是因为为了发生这种情况,你必须尝试使用​​'shouldAutorotate'作为设备旋转通知,但实际上并不使用操作系统的自动旋转function。 比如我做opengl游戏。 所以我只是让我的引擎知道旋转游戏graphics。 但我不是在旋转我的观点。 我不认为很多人使用这个设备轮换通知。

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

 - (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 ... 

然后回到我的视图控制器,我把所有的自动旋转的东西都没有。