最新文章
AppMobi发布HTML 5开发工具XDK
03-26 11:55
jqGrid的重要选项详解
03-26 11:52
解说HTML 5 语义化
03-26 11:50
HTML/CSS一些需要注意的基础知识
03-26 11:47
分享10个web前端简单实用的jQuery代码片段
03-26 11:42
分享七大无需编程的DIY开发工具
03-26 11:33
讲解如何禁止iOS屏幕自动旋转
2015年03月24日 16:54
0 点赞
0 评论
更新于 2025-11-21 18:30
在重力感应游戏中,屏幕自动旋转是一个较为常见的问题。屏幕自动旋转可能导致屏幕倒置,给玩家的操作带来极大不便。下面将详细介绍禁止重力感应(即禁止屏幕自动旋转)的具体方法。
操作步骤
需要在项目文件 “项目/ios/RootViewController.mm” 中找到如下函数:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// return UIInterfaceOrientationIsPortrait( interfaceOrientation );
#if defined(DISABLE_AUTO_ROTATE_ON_IPAD) && DISABLE_AUTO_ROTATE_ON_IPAD != 0
return interfaceOrientation == UIInterfaceOrientationPortrait;
#else
return YES;
#endif
} else {
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
}
代码解释
- 此函数用于判断是否允许设备旋转到指定的界面方向。
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad用于判断当前设备是否为 iPad。- 若为 iPad:
- 当定义了
DISABLE_AUTO_ROTATE_ON_IPAD且其值不为 0 时,只有当界面方向为竖屏(UIInterfaceOrientationPortrait)时才允许旋转,即禁止其他方向的自动旋转。 - 若未定义或其值为 0,则允许所有方向的自动旋转。
- 若不是 iPad,则只允许竖屏方向的旋转,禁止其他方向的自动旋转。
通过上述代码的设置,就可以实现禁止 iOS 屏幕自动旋转的功能,避免因屏幕自动旋转给操作带来的不便。