【H5+ & Quick-cocos2dx整合】之iOS 五 横竖屏切换处理

ios屏幕视图分为竖屏视图和横屏视图,横屏视可以旋转而竖屏视图则不可以。在viewcontroller中有三个系统级的方法来控制当前屏幕视图的操作。

shouldAutorotate:是否可以旋

supportedInterfaceOrientations:支持的旋转方向有哪些;

preferredInterfaceOrientationForPresentation:默认的屏幕方向设置

这三个方法用于ios6,之前版本用shouldAutorotateToInterfaceOrientation来控制视图方向。

cocos2dx的屏幕视图控制

cocos2dx在创建项目时,在项目根目录生成config.json,该文件是用来配置屏幕视图的,其中参数isLandscape就是用来配置是否横屏显示视图。

RootViewController.mm是cocos的视图控制类,该类重写shouldAutorotate和supportedInterfaceOrientations,并根据配置信息来控制视图显示,如下:

- (BOOL) shouldAutorotate {

if (ConfigParser::getInstance()->isLanscape()) {

return YES;// isLandscapetrue时,表示横屏,则可以旋转视图

}else{

return NO;

}

}

- (NSUInteger)supportedInterfaceOrientations{

#ifdef __IPHONE_6_0

if (ConfigParser::getInstance()->isLanscape()) {

return UIInterfaceOrientationMaskLandscape;

}else{

return UIInterfaceOrientationMaskPortraitUpsideDown;

}

#endif

}

:ConfigParser是针对config.json配置内容的解析工具类,提供配置信息的解析和读写方法。

H5+ SDK的屏幕视图控制

H5 plus的视图方向控制经过PDRCoreSettings的封装,他的配置信息存储在info.plist中。由于sdk非开放源码,只能看到接口定义。

PDRCoreSettings接口如下:

//加载配置文件

- (void) load;

// info.plist中支持的方向

- (BOOL)configSupportOrientation:(UIInterfaceOrientation)orientation ;

//判断是否支持指定的方向

- (BOOL) supportsOrientation:(UIInterfaceOrientation)orientation;

//判断所有支持的方向

- (NSUInteger)supportedInterfaceOrientations;

//设置支持的方向

- (void) setlockOrientation:(NSUInteger)orientation;

H5ViewController.mm是html5 plus sdk的视图控制类,重写了supportedInterfaceOrientations,并读取info.plist的方向配置来供系统调用。

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return [[PDRCore Instance].settings supportedInterfaceOrientations];

}

由于需求要求该视图必须是竖屏视图,所以该视图只能支持竖屏,解决方法有两种:

第一是修改info.plist,配置只支持竖屏;

第二是修改代码

UIInterfaceOrientationMaskPortrait

替换

[[PDRCore Instance].settings supportedInterfaceOrientations];

综上所述是ios的视图方向控制的工作逻辑,这些功能使用是前提是需要让该应用支持相应的视图方向。设置方法:target -> General -> Deployment Info -> Device Orientation

勾选相应的选项。

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...