- Creator 版本:3.4.2
- 目标平台: Web 平台
- 重现概率: 必现
在 engine\cocos\core\platform\view.ts
/**
* @en
* Sets the callback function for `view`'s resize action,<br/>
* this callback will be invoked before applying resolution policy, <br/>
* so you can do any additional modifications within the callback.<br/>
* Useful only on web.
* @zh 设置 `view` 调整视窗尺寸行为的回调函数,
* 这个回调函数会在应用适配模式之前被调用,
* 因此你可以在这个回调函数内添加任意附加改变,
* 仅在 Web 平台下有效。
* @param callback - The callback function
*/
public setResizeCallback (callback: (()=> void) | null) {
if (typeof callback === 'function' || callback == null) {
this._resizeCallback = callback;
}
}
并没有提示这个重复设置会覆盖之前的设置, 也无法删除, 我也是踩坑研究了下
这里给出我的封装, 建议加注释或封装下也可以
/** 回调 */
private _reSizeCallBackList: { method: Function, target: any }[] = []
public init(): void {
view.setResizeCallback(this.reSizeCallBack.bind(this))
}
/**
* 添加回调函数和触发对象
* @param method
* @param target
*/
public reSizeCallBackAdd(method: Function, target: any): void {
if (this._reSizeCallBackList.findIndex(item => item.method === method) === -1) {
this._reSizeCallBackList.push({ method, target })
}
}
/**
* 添加回调函数和触发对象
* @param method
* @param target
*/
public reSizeCallBackDel(method: Function): void {
const index = this._reSizeCallBackList.findIndex(item => item.method === method)
if (index !== -1) {
this._reSizeCallBackList.splice(index, 1)
$g.log('ViewManager 删除全屏回调')
}
}
private reSizeCallBack(): void {
this.reSetViewSize()
let i = this._reSizeCallBackList.length
while (--i > -1) {
if (this._reSizeCallBackList.length > i) {
const item = this._reSizeCallBackList[i]
item.method.apply(item.target)
}
}
}