【发布时间】:2011-03-11 07:27:04
【问题描述】:
为了弄清楚这一点,我进行了漫长而艰苦的搜索。我花了大约 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;
}
好的,那么这里发生了什么?我的主视图控制器只想处于 LandscapeRight 模式。所以它保持这样,因为它从不注册任何其他方向来自动旋转。我的游戏视图控制器需要能够旋转。如果我对 shouldAutorotateToInterfaceOrientation 使用以下代码
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
当 IPHONE 改变方向时它不会旋转。这是因为它的超级视图说它不想旋转。一旦超级视图返回 no,子视图甚至不会被询问。所以相反,我们游戏视图控制器只是说不,我不想自动旋转到任何视图。然后我们注册一个方向改变的通知,并调用我们自己的旋转方法。
最后这些代码是您自己处理旋转的方式。从 didRotate 内部调用 rotateView。请注意,我只希望用户在选择难度后能够旋转,所以我的 didRotate 方法中有逻辑来确定它是否应该旋转。代码中的 cmets 会解释为什么会发生某些事情。
-(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;
}
}
我希望这对某人有所帮助,我知道我在使用它时非常艰难,尤其是键盘从横向变为纵向。
【问题讨论】:
-
此评论与方向无关,但我正在查看您的应用程序,就像您用于短语“让我们这样做!”的字体一样那是什么字体?
-
字体被称为“Blade 2”(我猜是受电影启发)。
标签: iphone ipad ipod-touch