当前位置: 移动技术网 > IT编程>移动开发>IOS > ios开发教程_横竖屏切换 cocos2dx

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

2018年11月27日  | 移动技术网IT编程  | 我要评论

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"/>

如对本文有疑问, 点击进行留言回复!!

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网