看了别人的帖子,最开始用了这个方法,但是并不能贴合,而且创建出来的按钮的宽高并不对。
【
let btnSize = size(btnNode.getComponent(UITransform).width,btnNode.getComponent(UITransform).height);
console.log("btnSize: ", btnSize);
console.log("btnNode.position: ", btnNode.position);
let frameSize = view.getFrameSize();
let winSize = screen.windowSize;
console.log("winSize: ",winSize);
console.log("frameSize: ",frameSize);
//适配不同机型来创建微信授权按钮
let left = (winSize.width0.5+btnNode.position.x-btnSize.width0.5)/winSize.widthframeSize.width;
let top = (winSize.height0.5-btnNode.position.y-btnSize.height0.5)/winSize.heightframeSize.height;
let width = btnSize.width/winSize.widthframeSize.width;
let height = btnSize.height/winSize.heightframeSize.height;
console.log("button pos: ",v2(left,top));
console.log("button size: ",size(width,height));
】
后来又发现这个代码可以试试:
【
const btnSize = cc.size(btnNode.width + 0, btnNode.height + 0);
const frameSize = cc.view.getFrameSize();
const winSize = cc.director.getWinSize();
const designSize = cc.view.getDesignResolutionSize()
console.log('winSize: ', winSize);
console.log('frameSize: ', frameSize);
// /////////////////////////////////////////////////////////////////////////////////////////////////////
// 适配不同机型来创建微信授权按钮
let rect = btnNode.getBoundingBoxToWorld()
let ratio = cc.view.getDevicePixelRatio()
let scale = cc.view.getScaleX()
let factor = scale / ratio
let offsetX = 0
let offsetY = 0
if (cc.Canvas.instance.fitWidth) {
offsetY = (frameSize.height - designSize.height * factor) / 2
}
else {
offsetX = (frameSize.width - designSize.width * factor) / 2
}
let top = frameSize.height - (rect.y + rect.height) * factor - offsetY
let left = rect.x * factor + offsetX
// /////////////////////////////////////////////////////////////////////////////////////////////////////
const width = btnSize.width / winSize.width * frameSize.width;
const height = btnSize.height / winSize.height * frameSize.height;
console.log('button pos: ', cc.v2(left, top));
console.log('button size: ', cc.size(width, height));
// 计算授权按钮位置
】
发现按钮大小还是不对,可能我是高度适配原因吧,最终,我改了下就可以了:
【
private createAuthorizeBtn(btnNode:Node) {
let btnNodeUiTransform = btnNode.getComponent(UITransform);
const btnSize = size(btnNodeUiTransform.width + 0, btnNodeUiTransform.height + 0);
const frameSize = view.getFrameSize();
const winSize = screen.windowSize;
const designSize = view.getDesignResolutionSize();
console.log(“designSize”, designSize);
console.log('winSize: ', winSize);
console.log('frameSize: ', frameSize);
// /////////////////////////////////////////////////////////////////////////////////////////////////////
// 适配不同机型来创建微信授权按钮
let rect = btnNodeUiTransform.getBoundingBoxToWorld();
let ratio = screen.devicePixelRatio;
let scale = view.getScaleX();
let factor = scale / ratio;
let offsetX = 0;
let offsetY = 0;
// if (Canvas.instance.fitWidth) {
// offsetY = (frameSize.height - designSize.height * factor) / 2
// }
// else {
// offsetX = (frameSize.width - designSize.width * factor) / 2;
// }
let top = frameSize.height - (rect.y + rect.height) * factor - offsetY;
let left = rect.x * factor + offsetX;
// /////////////////////////////////////////////////////////////////////////////////////////////////////
const width = btnSize.width * factor;
const height = btnSize.height * factor;
console.log('button pos: ', v2(left, top));
console.log('button size: ', size(width, height));
let self = this;
self.btnAuthorize = wx.createUserInfoButton({
type: 'text',
text: '',
style: {
left: left,
top: top,
width: width,
height: height,
// lineHeight: 0,
backgroundColor: 'rgba(255, 255, 255, 0.5)',
// backgroundColor: '',
// color: '#ffffff',
// textAlign: 'center',
// fontSize: 16,
// borderRadius: 4
}
})
self.btnAuthorize.onTap((uinfo) => {
console.log("onTap uinfo: ",uinfo);
if (uinfo.userInfo) {
self.btnAuthorize.hide();
console.log("wxLogin auth success");
wx.showToast({title:"授权成功"});
this.node.active = false;
let path = `${Constant.RESOURCES_FILE_NAME.PREFAB}/${Constant.PREFAB_NAME.UIStart}`;
UIMgr.GetInstance().openPage(path);
}else {
console.log("wxLogin auth fail");
wx.showToast({title:"授权失败"});
}
});
}
】
不知道为啥 我的并不需要那个offset偏移 反正发出来给需要的人吧 大家都少走弯路。