应该在iOS 8中进行动作 [英] shouldAutorotate behavior in iOS 8
问题描述
我在 UIViewController shouldAutorotate
方法中发现7.1和8之间的小行为更改。
Apple View Controller编程指南指出在执行任何自动旋转之前调用此方法。
I found a small behavior change between 7.1 and 8 on the UIViewController shouldAutorotate
method.
The Apple View Controller Programming Guide states that This method is called before performing any autorotation.
但是我注意到当我只是禁用shouldAutorotate(返回NO)时,该方法在Portrait - > Landscape rotation上调用,但不在以下Landscape - > Portrait rotation上调用。同样应该始终按照我的理解调用该方法。在iOS 8上运行时会发生这种情况,但在iOS 7.1上运行时不会发生这种情况。
However I noticed that when I simply disable shouldAutorotate (return NO), the method is called on Portrait -> Landscape rotation, but not on the following Landscape -> Portrait rotation. Again the method should always be called as I understand it. This occurs when running on iOS 8, but not on iOS 7.1.
这似乎与iOS8中调用堆栈中的新方法有关:
This seems related to a new method in the call stack in iOS8 :
[UIWindow shouldAutorotateToInterfaceOrientation:checkForDismissal:isRotationDisabled]
我找不到有没有任何有关此方法及其行为的描述,我可以在哪里找到有关此内部方法的更多信息?
I could not find Is there any description of this method and its behavior, any idea where I could find more information about this internal method ?
重现这一点的简单步骤:
Simple steps to reproduce this:
- 创建一个新的 Single在Xcode中查看应用程序项目
-
更新
ViewController.m
以实施shouldAutorotate
- Create a new Single View Application project in Xcode
Update your
ViewController.m
to implement 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;
}
在模拟器上运行iPhone 5s(7.1):在切换到横向并返回到纵向时调用shouldAutorotate
推荐答案
同样的事情发生在我身上。我觉得iOS8中有一个错误,因为我的相同代码在iOS7中运行良好。另外,作为一个用户,我在我使用的应用程序中遇到了很多旋转问题。在8 shouldAutorotate
调用时,当你旋转到一侧,但是当你返回到正常旋转时不再调用。这是除非它实际上对侧面进行了自动旋转,在这种情况下,它被称为返回。
Same thing was happening to me. I have a feeling there is a bug in iOS8 because my same code worked fine in iOS7. Also just as a user I was have a lot of rotate problems in the apps I use. In 8 shouldAutorotate
calls when you rotate to the side but then not again when you return to a normal rotation. That is unless it actually did an autorotation to the side, in which case on the return it is called.
所以这是因为你的`shouldAutorotate'总是返回'NO' 。我的假设是,出于某种原因,在iOS8中,他们(改变这种行为的苹果编码器)决定如果他们在旋转到侧面时得到NO,他们在旋转回到肖像时不需要调用shouldAutorotate。所以它打破了我们期待的行为。
So it's because your `shouldAutorotate' is always returning 'NO'. My assumption is that for some reason in iOS8 they (the apple coders who changed this behavior) decided that if they got a NO when rotating to the side, they don't need to call shouldAutorotate when rotating back to portrait. So it broke the behavior that we were expecting.
没有人在谈论它或抱怨。 Google几乎没有针对此确切方案返回任何结果。我认为这是因为为了实现这一点,你必须尝试使用'shouldAutorotate'作为设备轮换通知,但实际上并没有使用操作系统的自动旋转功能。例如,我做opengl游戏。所以我只是让我的引擎知道旋转游戏图形。但我不是在转动我的观点。我认为很多人都没有将它用于设备轮换通知。
No one is talking about it or complaining. Google returns almost no results for this exact scenario. I think that's because in order for this to happen you have to be trying to use 'shouldAutorotate' as a device rotation notification but NOT actually be using the OS's auto rotation features. For example, I do opengl games. So I just this to let my engine know to rotate the game graphics. But I'm not rotating my view. I don't think many people were using this for device rotation notifications.
所以我决定使用设备通知。在我的视图控制器中:
So I decided to use device notifications instead. In my view controller:
- (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。
And then back in my view controller I turned all auto rotation stuff to NO.
这篇关于应该在iOS 8中进行动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!