横竖屏切换

ts:

    // "V" "H"
    setOrientation(dir)
    {
        cc.director.pause();

        let methodName = "setOrientation";
        if (cc.sys.os == cc.sys.OS_WINDOWS) {
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            methodName = methodName + ":";
            jsb.reflection.callStaticMethod("AppController", methodName, dir);
        } else if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", methodName, "(Ljava/lang/String;)V",dir);
        }

        let frameSize = cc.view.getFrameSize();
        if (dir == "V") {//竖屏
            if (frameSize.width > frameSize.height){
                cc.view.setFrameSize(frameSize.height,frameSize.width);
            }
            cc.view.setDesignResolutionSize(720, 1280, cc.ResolutionPolicy.FIXED_HEIGHT);
        }else if(dir == "H") {//横屏
            if (frameSize.height > frameSize.width){
                cc.view.setFrameSize(frameSize.height,frameSize.width);
            }
            cc.view.setDesignResolutionSize(1280, 720, cc.ResolutionPolicy.FIXED_HEIGHT);
        }
                
        cc.director.resume();
    }

 

Android:

    public static AppActivity s_activity = null;

    s_activity = this;

    public static void setOrientation(String dir)
    {
        if (dir.equals("H")) {
            s_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else if (dir.equals("V")) {
            s_activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }
    }

 

ios:

ccc选中方向:

2df801b315c938def676a4ad3fb703b01aa.jpg

xcode选中方向:

a3791383f57ef5efb884085e48f0d90eb18.jpg

AppController.mm

static AppController* s_sharedAppController;

s_sharedAppController = self;


+ (void)setOrientation:(NSString*)dir
{
    if ([dir isEqualToString:@"H"]) {
        s_sharedAppController->_viewController.orientationMask = UIInterfaceOrientationMaskLandscapeRight;
        s_sharedAppController->_viewController.orientation = UIInterfaceOrientationLandscapeRight;
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationUnknown] forKey:@"orientation"];
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];

    } else if ([dir isEqualToString:@"V"]) {        
        s_sharedAppController->_viewController.orientationMask = UIInterfaceOrientationMaskPortrait;
        s_sharedAppController->_viewController.orientation = UIInterfaceOrientationPortrait;
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationUnknown] forKey:@"orientation"];
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
    }
}

 

RootViewController.h

@property(nonatomic, assign) UIInterfaceOrientationMask orientationMask;
@property(nonatomic, assign) UIInterfaceOrientation orientation;

RootViewController

    _orientationMask = UIInterfaceOrientationMaskPortrait;
    _orientation = UIInterfaceOrientationPortrait;

 

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
#ifdef __IPHONE_6_0
- (NSUInteger) supportedInterfaceOrientations{
    //return UIInterfaceOrientationMaskAllButUpsideDown;
    return _orientationMask;
}
#endif

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return _orientation;
}

转载于:https://my.oschina.net/valiancer/blog/2996186

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值