频道栏目
首页 > 资讯 > IOS > 正文

ios开发教程_横竖屏切换 cocos2dx

17-06-14        来源:[db:作者]  
收藏   我要投稿

ios开发教程_横竖屏切换 cocos2dx。

项目设置DeploymentInfo ->Device Orientation都不勾选

AppController.mm

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

return UIInterfaceOrientationMaskAllButUpsideDown;

}

RootViewController.h

+(void)changeDir:(bool)bIsLeft;

RootViewController.mm

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

return UIInterfaceOrientationIsLandscape( interfaceOrientation );

}

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead

- (NSUInteger) supportedInterfaceOrientations{

#ifdef __IPHONE_6_0

return UIInterfaceOrientationMaskAllButUpsideDown;

#endif

}

static bool s_needOrientation=false;

+(void)changeDir:(bool)bIsLeft{

if(s_needOrientation == bIsLeft )

return;

s_needOrientation = bIsLeft ;

[[UIDevice currentDevice] performSelector:@selector(setOrientation:)

withObject:

bIsLeft ? (id)UIDeviceOrientationLandscapeLeft : (id)UIDeviceOrientationPortrait];

}

- (BOOL) shouldAutorotate {

//bool bIsPortraitCur = ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft);

bool bIsLeftCur=([UIDevice currentDevice].orientation==UIDeviceOrientationPortrait);

//CCLOG("s_needOrientation:%d",s_needOrientation);

//CCLOG("bIsPortraitCur:%d",bIsLeftCur);

if( bIsLeftCur != s_needOrientation )

return YES;

else

return NO;

}

//这是replaceScene时候的页面(先调用changeDirection,后转向需要转屏的页面)

h文件

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

#include "RootViewController.h"

#endif

//声明转屏方法

void changeDirection();

cpp文件,需要把cpp文件后缀改成mm

竖屏

void Scene2::changeDirection(){

#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS

[RootViewController changeDir:true];

auto director = Director::getInstance();

auto glview = director->getOpenGLView();

cocos2d::Size frameSize=glview->getFrameSize();

glview->setFrameSize(frameSize.height,frameSize.width);

director->getOpenGLView()->setDesignResolutionSize(1136.0f, 640.0f, ResolutionPolicy::EXACT_FIT);

#endif

}

横屏

void Scene1::changeDirection(){

#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS

[RootViewController changeDir:false];

auto director = Director::getInstance();

auto glview = director->getOpenGLView();

cocos2d::Size frameSize=glview->getFrameSize();

glview->setFrameSize(frameSize.height,frameSize.width);

director->getOpenGLView()->setDesignResolutionSize(640.0f,1136.0f, ResolutionPolicy::EXACT_FIT);

#endif

}

=======================================================

=======================android=======================

1.

修改目录下的:proj.android\src\org\cocos2dx\lib\Cocos2dxActivity.java

添加下面3个 方法

//横屏

public static void ChangeLand(){

sContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}

//竖屏

public static void ChangePort(){

sContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

//获得当前屏幕方向

public static int getDirection(){

//int strDirec=sContext.getRequestedOrientation();

//Log.e("direction",new String(strDirec+""));

//System.out.println(strDirec);

return sContext.getRequestedOrientation();

}

2.

//这是replaceScene时候的页面(先调用changeDirection,后转向需要转屏的页面)

h文件添加

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID

#include

#include "platform/android/jni/JniHelper.h"

#endif

//声明转屏方法

void changeDirection();

cpp文件添加

//竖屏转横屏

extern "C" //因为jni将java代码转过来是c的,所以C++引用得加上

{

void ChangeLand();

}

void Scene2::changeDirection(){

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

//判断当前设备的方向

//如果是横向就不做操作,竖向就强制横屏

JniMethodInfo info;

if(JniHelper::getStaticMethodInfo(info,"org/cocos2dx/lib/Cocos2dxActivity","getDirection","()I")){

jint iret=info.env->CallStaticIntMethod(info.classID,info.methodID);

if(iret==1){

if(JniHelper::getStaticMethodInfo(info,"org/cocos2dx/lib/Cocos2dxActivity","ChangeLand","()V"))

{

info.env->CallStaticVoidMethod(info.classID,info.methodID);

}

auto director = Director::getInstance();

auto glview = director->getOpenGLView();

Size frameSize=glview->getFrameSize();

glview->setFrameSize(frameSize.height,frameSize.width);

director->getOpenGLView()->setDesignResolutionSize(1136.0f, 640.0f, ResolutionPolicy::EXACT_FIT);

}

}

#endif

}

//横屏转竖屏

extern "C" //因为jni将java代码转过来是c的,所以C++引用得加上

{

void ChangePort();

}

void Scene1::changeDirection(){

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

//判断当前设备的方向

//如果是竖向就不做操作,横向就强制竖屏

JniMethodInfo info;

if(JniHelper::getStaticMethodInfo(info,"org/cocos2dx/lib/Cocos2dxActivity","getDirection","()I")){

jint iret=info.env->CallStaticIntMethod(info.classID,info.methodID);

if(iret==0){

if(JniHelper::getStaticMethodInfo(info,"org/cocos2dx/lib/Cocos2dxActivity","ChangePort","()V"))

{

info.env->CallStaticVoidMethod(info.classID,info.methodID);

}

auto director = Director::getInstance();

auto glview = director->getOpenGLView();

Size frameSize=glview->getFrameSize();

glview->setFrameSize(frameSize.height,frameSize.width);

glview->setDesignResolutionSize(640.0f, 1136.0f, ResolutionPolicy::EXACT_FIT);

}

}

#endif

}

3.

如在windows下调试

//横屏

#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32

auto director = Director::getInstance();

auto glview = director->getOpenGLView();

glview->setFrameSize(656.0f,370.0f);//自定义的大小,1136*640的话太大

director->getOpenGLView()->setDesignResolutionSize(1136.0f, 640.0f, ResolutionPolicy::EXACT_FIT);

#endif

//竖屏

#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32

auto director = Director::getInstance();

auto glview = director->getOpenGLView();

glview->setFrameSize(370.0f,656.0f);

//Size frameSize=glview->getFrameSize();

glview->setDesignResolutionSize(640.0f,1136.0f, ResolutionPolicy::EXACT_FIT);

#endif

最后附上AndroidMainfest.xml

package="org.cocos.CocosProject"

android:versionCode="1"

android:versionName="1.0"

android:installLocation="auto">

android:icon="@drawable/icon">

android:value="cocos2dcpp" />

android:label="@string/app_name"

android:screenOrientation="portrait"

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

android:configChanges="orientation|keyboardHidden">

android:smallScreens="true"

android:normalScreens="true"

android:largeScreens="true"

android:xlargeScreens="true"/>

相关TAG标签
上一篇:MOOC清华《程序设计基础》第1章第5题:银行存款问题
下一篇:第四届“图灵杯”NEUQ-ACM程序设计竞赛(团队赛)-网络同步赛B(排序)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站