我花了很长时间努力寻找答案,最终花费了4-5个小时才得以解决。我将回答自己的问题,以便为遇到同样问题的任何人提供支持。
所以,我创建了一个名为Critical Mass Typer的应用程序。它是一款打字游戏,使用键盘作为主要控制方法。单词向屏幕中心移动,在那里你有一个核。你输入单词,并在它到达你的核之前将其击败。如果有4个单词到达你的核,你就会达到临界质量并爆炸(游戏结束)。
现在,我想能够在游戏中支持横向和纵向模式。我事先考虑过,并设计了我的类,使其易于实现。设置游戏来这样做并不是问题。正确旋转视图则是问题所在。
很快出现的一个问题是:任何视图控制器的shouldAutorotateToInterfaceOrientation方法返回NO,都会取消其子视图的返回值。
因此,我的主视图控制器(CriticalMassViewController)控制游戏的启动/菜单,并添加一个运行游戏模式(CMGameViewController)的视图控制器作为子视图。我只希望我的主视图控制器处于横向模式,因为如果我旋转它,动画/按钮都会跑到侧面。如果我想要将其设置为纵向模式,我必须创建另一个视图或更改屏幕上所有内容的位置。然而,我的游戏视图需要能够切换。为了实现这一点,你需要将返回值设置为你希望你的主视图所在的特定模式。然后告诉你的游戏视图不返回任何模式。之后,注册游戏视图以获取通知,然后自己处理旋转。
例如:
CriticalMassViewController.m: -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
CMGameViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
/*GUI setup code was here*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)didRotate:(NSNotification *)nsn_notification {
UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
/*Rotation Code
if(interfaceOrientation == UIInterfaceOrientationPortrait) {
//Portrait setup
} else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
//Landscape setup
}
*/
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return NO;
}
好的,这里发生了什么?我的主视图控制器只想保持横屏模式。因此,它会一直保持这种模式,因为它从未注册其他方向进行自动旋转。我的游戏视图控制器需要能够旋转。如果我使用以下代码来处理 shouldAutorotateToInterfaceOrientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
当iPhone改变方向时,它不会旋转。这是因为它的父视图表示不想旋转。一旦父视图返回否,子视图甚至都不会被询问。因此,我们游戏视图控制器只需说“不”,我不想自动旋转到任何视图。然后,我们注册通知以便在方向更改时调用自己的旋转方法。
这些代码片段是如何处理自己的旋转的。从didRotate中调用rotateView。请注意,我只希望用户在选择难度后才能旋转,因此我在didRotate方法中有逻辑来确定是否应该旋转。代码中的注释将解释为什么发生某些事情。
-(void) rotateView:(UIInterfaceOrientation)uiio_orientation {
/*This line is very important if you are using the keyboard. This was actually the
largest problem I had during this entire ordeal. If you switch orientations while
the keyboard is already being displayed, it will stay in landscape orientation. To
get it to switch to portrait, we have to tell the status bar that its orientation
has changed. Once you do this, the keyboard switches to portrait*/
[UIApplication sharedApplication].statusBarOrientation = uiio_orientation;
//Get the view you need to rotate
UIView *portraitImageView = self.view;
/*Check to make sure that you are in an orientation that you want to adjust to. If
you don't check, you can make your view accidently rotate when the user orientates
their phone to a position you aren't expecting*/
if(uiio_orientation == UIInterfaceOrientationPortrait || uiio_orientation == UIInterfaceOrientationLandscapeRight) {
if(uiio_orientation == UIInterfaceOrientationPortrait) {
//This transform rotates the view 90 degrees counter clock wise.
portraitImageView.transform = CGAffineTransformMakeRotation(-M_PI/2);
} else if(uiio_orientation == UIInterfaceOrientationLandscapeRight) {
//This transform rotates the view back to its original position
portraitImageView.transform = CGAffineTransformMakeRotation(0);
}
CGRect frame = portraitImageView.frame;
frame.origin.y = 0;
frame.origin.x = 0;
frame.size.width = portraitImageView.frame.size.height;
frame.size.height = portraitImageView.frame.size.width;
portraitImageView.frame = frame;
}
}
我希望这篇文章能够帮助到一些人,因为我自己在这方面遇到了很大的困难,特别是当键盘从横屏切换到竖屏时。