updateRewardItem1() {
let content = this.content; // scrollview 的 content
let children = content.children; // 获取当前所有Item
// 当前item数量超过设定数量则开启复用
// foodItemMaxShowNum 一次性创建最大item数量 比如可视区域能放下6个 则最好大于等8
if (children.length >= this.foodItemMaxShowNum) {
let len = children.length;
let curIndex = children[len - 1][`index`] + 1; // 获取下一个Item应该设置的index
// 向上滑动,并且下一个curIndex小于总的item数量
// foodContentStartY item当前停止位置Y坐标
if (content.y - this.foodContentStartY > 0 && curIndex < this.foodNum) { // 往上滑动
let pos = content.convertToWorldSpaceAR(children[0].position);
let pos1 = this.scrollview.convertToNodeSpaceAR(pos); // 获取第一个item相对于scrollview的坐标
// 如果不在scrollview可视区域内,则移动到底部
if (pos1.y >= children[0].height + 30) {
// 更新item信息 param1 item param2 index param3 posY
this.updateItem(children[0], children[len - 1][`index`] + 1, children[len - 1].y - children[0].height - 30);
// 更改该item在content中的位置(最底层)
let item = children[0];
children[0].parent = null;
item.parent = content;
}
content.height += content.y - this.foodContentStartY; // 更新content高度
this.foodContentStartY = content.y; // 更新item停止位置Y
}
// 向下滑动,并且最上层item的index >= 0
else if (content.y - this.foodContentStartY < 0 && (children[0][`index`] - 1) >= 0) { // 往下拉
let pos = content.convertToWorldSpaceAR(children[len - 1].position);
let pos1 = this.scrollview.convertToNodeSpaceAR(pos);
// 如果不在scrollview可视区域内,则移动到顶部
if (pos1.y <= -(this.foodItemMaxShowNum - 2) * (children[0].height + 30)) {
// 更新item信息 param1 item param2 index param3 posY
this.updateItem(children[len - 1], children[0][`index`] - 1, children[0].y + children[0].height + 30);
// 更改该item在content中的位置(最上层)
let item = children[len - 1];
item.parent = null;
content.insertChild(item, 0);
}
content.height += content.y - this.foodContentStartY;// 更新content高度
this.foodContentStartY = content.y;// 更新item停止位置Y
}
}
}
demo
assets.zip (4.9 KB)

