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全平台横竖屏切换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VisualSVN Server使用手册
- 下一篇: 大数据技术助推数字化智慧城市管理平台的搭