日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cocoscreator固定旋转_cocoscreator全平台横竖屏切换

發布時間:2024/1/1 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cocoscreator固定旋转_cocoscreator全平台横竖屏切换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

筆者所使用的方案經歷了正式的線上項目使用,請放心食用

假設項目的設計分辨率為1920x1080

一、全平臺通用邏輯

無論是Android、IOS還是h5,進行橫豎屏切換,都需要執行下面對應代碼,

而h5執行之后,就能完美適配橫豎屏切換了。

Andriod、IOS 還需要一些其他的處理。

首先// 獲取屏幕物理分辨率

let frameSize = cc.view.getFrameSize()

豎屏cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT)

if (frameSize.width > frameSize.height)

cc.view.setFrameSize(frameSize.height, frameSize.width)

// this.canvas 為當前fire場景的畫板

this.canvas.designResolution = cc.size(1080, 1920)

橫屏cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE)

if (frameSize.height > frameSize.width)

cc.view.setFrameSize(frameSize.height, frameSize.width)

this.canvas.designResolution = cc.size(1920, 1080)

二、Android原生處理

豎屏// instance 為AppActivity,我的做法是做了對象?public static AppActivity instance,然后在 onCreate 賦值

instance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

橫屏// instance 為AppActivity,我的做法是做了對象?public static AppActivity instance,然后在 onCreate 賦值

instance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

上面的兩個函數,需要腳本層通過反射調用。

三、IOS原生處理// 首先,將設備方向設為未知,這段必要,否則直接切換橫豎屏,有可能會切換失敗

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationUnknown] forKey:@"orientation"];

float w = cocosView.bounds.size.width;

float h = cocosView.bounds.size.height;

豎屏oMask = UIInterfaceOrientationMaskPortrait;

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

if (w > h){

cocosView.bounds = CGRectMake(0, 0, h, w);

}

橫屏oMask = UIInterfaceOrientationMaskLandscape;

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];

if (h > w){

cocosView.bounds = CGRectMake(0, 0, h, w);

}

最后不管橫豎屏cocosView.center = CGPointMake(cocosView.frame.size.width * 0.5, cocosView.frame.size.height * 0.5);

四、Android、IOS 通用腳本處理

IOS、Android設備在屏幕旋轉的時候并不會觸發布局重新刷新,但是我們可以主動調用事件觸發刷新window.dispatchEvent(new cc.Event.EventCustom('resize', true))

為什么resize能刷新能,你可以在CCWidgetManager.js,找到如下代碼init: function init(director) {

...

if (cc.sys.isMobile) {

window.addEventListener('resize', this.onResized.bind(this));

}

...

}onResized: function onResized() {

var scene = cc.director.getScene();

if (scene) {

this.refreshWidgetOnResized(scene);

}

}

更詳細的自己去跟蹤引擎代碼吧。

至此全平臺的屏幕旋轉實現。

總結

以上是生活随笔為你收集整理的cocoscreator固定旋转_cocoscreator全平台横竖屏切换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。