Commit 3942b415 authored by Li MingZhe's avatar Li MingZhe

feat: 底部item 宽度适配

parent d896137d
...@@ -22,6 +22,7 @@ cc.Class({ ...@@ -22,6 +22,7 @@ cc.Class({
_maxPage: null, _maxPage: null,
_lineMaxCount: null, _lineMaxCount: null,
_disW: null, _disW: null,
_maxW: null,
onLoad() { onLoad() {
this._canTouch = true; this._canTouch = true;
this._isTop = false; this._isTop = false;
...@@ -35,8 +36,8 @@ cc.Class({ ...@@ -35,8 +36,8 @@ cc.Class({
this._maxPage = 0; this._maxPage = 0;
this._disW = 5; this._disW = 5;
const maxW = 1280 - 90; this._maxW = 1280 - 90;
this._lineMaxCount = Math.floor(maxW / (this._disW + this._itemLen)); this._lineMaxCount = Math.floor(this._maxW / (this._disW + this._itemLen));
}, },
...@@ -112,7 +113,7 @@ cc.Class({ ...@@ -112,7 +113,7 @@ cc.Class({
return; return;
} }
if (this._pageIndex >= this._maxPage) { if (this._pageIndex >= this._maxPage - 1) {
return; return;
} }
this._pageIndex++; this._pageIndex++;
...@@ -142,7 +143,7 @@ cc.Class({ ...@@ -142,7 +143,7 @@ cc.Class({
this._pageUpBtn.opacity = 255; this._pageUpBtn.opacity = 255;
} }
if (this._pageIndex == this._maxPage) { if (this._pageIndex == this._maxPage - 1) {
this._pageDownBtn.opacity = 100; this._pageDownBtn.opacity = 100;
} else { } else {
this._pageDownBtn.opacity = 255; this._pageDownBtn.opacity = 255;
...@@ -185,12 +186,35 @@ cc.Class({ ...@@ -185,12 +186,35 @@ cc.Class({
return; return;
} }
this._dataArr = dataArr; this._dataArr = dataArr;
this._maxPage = Math.ceil(dataArr.length / this._lineMaxCount) - 1; // this._maxPage = Math.ceil(dataArr.length / this._lineMaxCount) - 1;
this._initPageArr();
this._pageIndex = 0; this._pageIndex = 0;
this._refreshPage(); this._refreshPage();
}, },
_pageArr: null,
_initPageArr() {
let tmpArr = [];
this._pageArr = [tmpArr];
let curLen = this._disW;
for (let i=0; i<this._dataArr.length; i++) {
const item = this._addOneItem(this._dataArr[i]);
curLen += item.width + this._disW;
if (curLen > this._maxW) {
tmpArr = [item];
this._pageArr.push(tmpArr);
} else {
tmpArr.push(item);
}
}
this._maxPage = this._pageArr.length;
console.log('this._maxPage: ', this._maxPage);
},
_itemLen: null, _itemLen: null,
_addOneItem(data) { _addOneItem(data) {
...@@ -207,6 +231,7 @@ cc.Class({ ...@@ -207,6 +231,7 @@ cc.Class({
this._addItemLabel(item, data.text); this._addItemLabel(item, data.text);
this._addItemAudio(item, data.audio_url); this._addItemAudio(item, data.audio_url);
item.data = data;
return item; return item;
}, },
...@@ -254,6 +279,10 @@ cc.Class({ ...@@ -254,6 +279,10 @@ cc.Class({
} }
label.font = this._labelFont; label.font = this._labelFont;
label._forceUpdateRenderData(true)
if (labelNode.width > this._itemLen) {
item.width = labelNode.width;
}
}, },
...@@ -286,20 +315,35 @@ cc.Class({ ...@@ -286,20 +315,35 @@ cc.Class({
const disW = this._disW; const disW = this._disW;
const startIndex = this._pageIndex * this._lineMaxCount; const startIndex = this._pageIndex * this._lineMaxCount;
const showArr = dataArr.slice(startIndex, startIndex + this._lineMaxCount); // const showArr = dataArr.slice(startIndex, startIndex + this._lineMaxCount);
const showArr = this._pageArr[this._pageIndex];
let baseX = this._getCurPageBaseX();
const baseX = -(disW + this._itemLen) * (showArr.length - 1) / 2;
// const baseX = -(disW + this._itemLen) * (showArr.length - 1) / 2;
const baseY = -25; const baseY = -25;
showArr.forEach((data, i) => { showArr.forEach((el, i) => {
const item = this._addOneItem(data); const item = this._addOneItem(el.data);
this._itemLayer.addChild(item); this._itemLayer.addChild(item);
item.x = baseX + (disW + this._itemLen) * i; item.x = baseX + (disW + item.width) / 2
item.y = baseY; item.y = baseY;
baseX = item.x + (disW + item.width) / 2;
}); });
this._setPageBtnState(); this._setPageBtnState();
}, },
_getCurPageBaseX() {
const arr = this._pageArr[this._pageIndex];
console.log('arr: ', arr);
let curLen = this._disW;
for (let i=0; i<arr.length; i++) {
curLen += (arr[i].width + this._disW)
}
return -curLen / 2;
},
// update (dt) {}, // update (dt) {},
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment