quick cocos2dx2.2.6 lua 屏幕旋转横竖屏转换详解 屏幕方向自由切换

这里指的是程序中代码控制横竖屏转换

 

1. 先调用ios/android的api使屏幕旋转

IOS

在ios平台,cocos2dx的视图其实都是显示在一个UIViewController的。所以我们需要旋转UIViewController的方向。

在ios8以前只需调用以下代码:

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    [UIViewController attemptRotationToDeviceOrientation];    

    s_self->viewController.view.transform = CGAffineTransformMakeRotation(M_PI/2);
    float width =s_self->viewController.view.bounds.size.width;
    float height =s_self->viewController.view.bounds.size.height;
    s_self->viewController.view.bounds = CGRectMake(0, 0, height, width);

但是在ios8以后这样做就不行了。苹果不推荐手动修改UIViewController的方向。

苹果推荐的是重力使得屏幕自动旋转然后修改UI布局;或者是在切换UIViewController,新UIViewController有新的方向。

ios8做法:新建一个RootViewController2,代码跟RootViewController一样,只修改设置方向的相关代码。

在AppController里引入并初始化RootViewController2

AppController.h:

...
@public
RootViewController *viewController; @public RootViewController2 *viewController2;
....

AppController.mm:

static AppController *s_self;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    s_self = self;
    ....// Use RootViewController manage EAGLView
    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    viewController.wantsFullScreenLayout = YES;
    viewController.view = __glView;
    
    viewController2 = [[RootViewController2 alloc] initWithNibName:nil bundle:nil];
    viewController2.wantsFullScreenLayout = YES;
    
    ....
}

切换RootViewController

+(void)changeRootViewController{
EAGLView *__glView = (EAGLView *)s_self->viewController.view; s_self->viewController.view = nil; s_self->viewController2.view = __glView; if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0) { [s_self->window addSubview:s_self->viewController2.view]; } [s_self->window setRootViewController:s_self->viewController2];

      [__glView setOriginalRect:__glView.frame];


    
    CCLOG("____________________________");
    CCLOG("glView  retainCount %lu",(unsigned long)[__glView retainCount]);
    CCLOG("viewController retainCount %lu",(unsigned long)[s_self->viewController retainCount]);
    CCLOG("viewController2 retainCount %lu",(unsigned long)[s_self->viewController2 retainCount]);
}

RootViewController.mm控制方向代码:

- (NSUInteger) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;    //横

     //UIInterfaceOrientationMaskLandscape  竖

}

Android

AndroidManifest.xml

..
<activity android:name=".XXX"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:screenOrientation="portrait"
            android:configChanges="orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
..

xxx.java 旋转屏幕

public static void changeOrientationToLandscape(){
        _this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
public static void changeOrientationToPortrait(){
        _this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);        
}

 2. 修改DesignResolutionSize和FrameSize

 

  
  -- 调用oc java代码修改方向
  if
device.platform == "ios" then luaoc.callStaticMethod("AppController", "changeToRootViewController") elseif device.platform == "android" then luaj.callStaticMethod("com/iwo/doudizhu/Doudizhu", "changeOrientationToPortrait") end -- 修改FrameSize width height互换 local frameSize = CCEGLView:sharedOpenGLView():getFrameSize() CCEGLView:sharedOpenGLView():setFrameSize(frameSize.height,frameSize.width) -- 修改DesignResolutionSize 修改lua display相关值 主要是width height互换 glview:setDesignResolutionSize(....)
  display...

 

转载于:https://www.cnblogs.com/junzitandandan/p/4317563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值