屏幕旋转实现方案,支持全平台

最近需要在我们横版游戏接入一个webview,而webview是竖版的,就鼓捣了下设备屏幕旋转,同时支持web,android,ios平台,下面是实现方案(初始方向为横版):

  1. 设置屏幕旋转 cc.view.setOrientation(),这个API的描述文档写着对native无效,实际上还是有影响的
  2. 设置framesize为对应方向的尺寸 cc.view.setFrameSize()
  3. 更改canvas的 designResolution 和其他参数

WEB平台: web平台本身就支持旋转,执行上面步骤就可以完美适配

ANDROID平台(AppActivity.java里添加以下代码,需要import android.content.pm.ActivityInfo;):

public static void setOrientation(String dir){
    if(dir.equals("V"))
        ((AppActivity)(SDKWrapper.getInstance().getContext())).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    else
        ((AppActivity)(SDKWrapper.getInstance().getContext())).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}

((AppActivity)(SDKWrapper.getInstance().getContext()))只是为了拿到AppActivity的实例,你也可以存个变量


IOS平台(AppController.mm里添加以下代码):

UIInterfaceOrientationMask oMask = UIInterfaceOrientationMaskLandscape;

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return oMask;
}

+(void)setOrientation:(NSString*)dir{
    if([dir isEqualToString:@"V"]){
        oMask = UIInterfaceOrientationMaskPortrait;
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
    }
    else{
        oMask = UIInterfaceOrientationMaskLandscape;
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
    }
}

ios平台的查了些资料,发现好多实现都是需要同时勾选设备的横向、竖向,最后才发现用supportedInterfaceOrientationsForWindow可以很容易实现。
oMask的初始值要与你项目的Device Orientation相同,我的demo中是勾选了 LandScape Left 和 LandScape Right。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值