ios屏幕视图分为竖屏视图和横屏视图,横屏视可以旋转而竖屏视图则不可以。在viewcontroller中有三个系统级的方法来控制当前屏幕视图的操作。
shouldAutorotate:是否可以旋转;
supportedInterfaceOrientations:支持的旋转方向有哪些;
preferredInterfaceOrientationForPresentation:默认的屏幕方向设置
这三个方法用于ios6,之前版本用shouldAutorotateToInterfaceOrientation来控制视图方向。
cocos2dx的屏幕视图控制
cocos2dx在创建项目时,在项目根目录生成config.json,该文件是用来配置屏幕视图的,其中参数isLandscape就是用来配置是否横屏显示视图。
RootViewController.mm是cocos的视图控制类,该类重写shouldAutorotate和supportedInterfaceOrientations,并根据配置信息来控制视图显示,如下:
- (BOOL) shouldAutorotate {
if (@H_403_37@ConfigParser::@H_403_37@getInstance()->@H_403_37@isLanscape()) {
return YES;// isLandscape为true时,表示横屏,则可以旋转视图
}else{
return NO;
}
}
- (@H_403_37@NSUInteger)supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
if (@H_403_37@ConfigParser::@H_403_37@getInstance()->@H_403_37@isLanscape()) {
return @H_403_37@ UIInterfaceOrientationMaskLandscape;
}else{
return @H_403_37@ UIInterfaceOrientationMaskPortraitUpsideDown;
}
#endif
}
注:ConfigParser是针对config.json配置内容的解析工具类,提供配置信息的解析和读写方法。
H5+ SDK的屏幕视图控制
H5 plus的视图方向控制经过PDRCoreSettings的封装,他的配置信息存储在info.plist中。由于sdk非开放源码,只能看到接口定义。
PDRCoreSettings接口如下:
//加载配置文件
- (void) load;
// info.plist中支持的方向
- (BOOL)configSupportOrientation:(@H_403_37@UIInterfaceOrientation)orientation ;
//判断是否支持指定的方向
- (BOOL) supportsOrientation:(@H_403_37@UIInterfaceOrientation)orientation;
//判断所有支持的方向
- (@H_403_37@NSUInteger)supportedInterfaceOrientations;
//设置支持的方向
- (void) setlockOrientation:(@H_403_37@NSUInteger)orientation;
H5ViewController.mm是html5 plus sdk的视图控制类,重写了supportedInterfaceOrientations,并读取info.plist的方向配置来供系统调用。
- (@H_403_37@UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [[@H_403_37@PDRCore @H_403_37@Instance].@H_403_37@settings @H_403_37@supportedInterfaceOrientations];
}
由于需求要求该视图必须是竖屏视图,所以该视图只能支持竖屏,解决方法有两种:
UIInterfaceOrientationMaskPortrait
替换
[[@H_403_37@PDRCore @H_403_37@Instance].@H_403_37@settings @H_403_37@supportedInterfaceOrientations];
综上所述是ios的视图方向控制的工作逻辑,这些功能使用是前提是需要让该应用支持相应的视图方向。设置方法:target -> General -> Deployment Info -> Device Orientation
勾选相应的选项。