iOS默认启动方向

iOS开发的应用程序可以随意更改其手机方向(UIInterfaceOrientation),一般来说都由ViewController的-(BOOL)shouldAutorotateToInterfaceOrientation:负责帮你完成:

1 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
2 {
3     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
4 }

如果你在XCode中新建一个简单的Window-base Application,那么上述代码就完全可以帮你完成手机旋转方向时自动的调整整个视图的方向。不过需要注意的是,如果此时你观察工程的TARGETS,会在Summary下的Supported Device Orientations下发现Portrait、Landscape Left和Landscape Right三项是被选中的。

现在提出两个问题,如果你开发一个横向过关的ARPG游戏的话(类似以前的街机游戏《三国志》等),那么游戏启动的时候就必须是处于Landscape状态的,且不能成为Portrait状态。如果你开发一个纵向飞行射击游戏的话(类似以前的街机游戏《彩京1945》等),那么游戏启动的时候就必须是处于Portrait状态的,且不能成为Landscape状态。此时该怎么办呢?

对于上述问题,有两个要点,一是程序启动的时候就要按指定的方式(Landscape或者Portrait)启动,二是在程序启动后只能保持一种方向运行。

具体的解决方法(以下以横向过关游戏为例):

1.将工程的TARGETS中的Supported Device Orientations下的所有选项取消选择。

2.在TARGETS中的Info页中添加"Initial interface orientation"并制定未某一横向方向(例如Landscape(right home button))

3.修改shouldAutorotateToInterfaceOritentation函数,

1 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
2 {
3     return UIInterfaceOrientationIsLandscape(interfaceOrientation);
4 }

对于竖向的应用程序,由于Initial interface orientation默认的就是Portrait,所以可以不用设置此项,只需要修改shouldAutorotateToInterfaceOrientation函数即可实现启动为纵向游戏:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
posted @ 2012-08-06 15:49  芈希有  阅读(1884)  评论(0编辑  收藏  举报