CocosCreator | 橫豎屏切換

更多筆記和源碼請關注:【微信公衆號】 CocosCreator筆記

Step 1

適配的UI加上widget

 

Step 2

擼代碼

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    //V H    setOrientation(dir)    {        if (cc.sys.os == cc.sys.OS_ANDROID) {            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'setOrientation', '(Ljava/lang/String;)V', dir);
        } else if (cc.sys.os == cc.sys.OS_IOS) {            jsb.reflection.callStaticMethod('AppController', 'setOrientation:', dir);        }
        let frameSize = cc.view.getFrameSize();
        if (dir == 'V') {            cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);            if (frameSize.width > frameSize.height) {                cc.view.setFrameSize(frameSize.height, frameSize.width);            }            cc.Canvas.instance.designResolution = cc.size(750, 1334);        } else {            cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);            if (frameSize.height > frameSize.width) {                cc.view.setFrameSize(frameSize.height, frameSize.width);                cc.Canvas.instance.designResolution = cc.size(1334, 750);            }        }
        if (CC_JSB) {            window.dispatchEvent(new cc.Event.EventCustom('resize', true));            }    }

 

Step 3

Android部分

構建時設備方向爲遊戲初始方向

修改AppActivity.java文件

  •  
  •  
  •  
  •  
  •  
  •  
  •  
    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);        }    }

 

Step 4

ios部分

構建時設備方向爲遊戲初始方向

ios工程設備方向也爲遊戲初始方向,不需要添加其他的

修改AppController.mm文件

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
UIInterfaceOrientationMask orientation = UIInterfaceOrientationMaskLandscape;
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {    return orientation;}
+ (void)setOrientation:(NSString*)dir {    if ([dir isEqualToString:@"V"]) {        orientation = UIInterfaceOrientationMaskPortrait;        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];    } else {        orientation = UIInterfaceOrientationMaskLandscape;        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];    }}
發佈了30 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章