gpt4 book ai didi

c++ - Cocos2d-x中如何判断ScrollView结束?

转载 作者:行者123 更新时间:2023-11-28 05:54:19 26 4
gpt4 key购买 nike

上下文
我通过 ui::ListView 制作了一个自定义排行榜。排行榜的每个元素都是 ui::Button。当用户点击某个位置时,他会在排行榜中收到有关该位置的详细统计信息。我正在使用 Cocos2d-x 版本。 3.8.1.为了清楚起见,这是我的代码:

    auto lvLeaderboard = ListView::create();
for (int j = 0; j < linesCount; j++)
{
Button* btnSingleUser = Button::create("btn.png”);
btnSingleUser->setTag(j + offsetResult);
btnSingleUser->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
switch (type)
{
case ui::Widget::TouchEventType::ENDED:
{
Button* currentButton = (Button*)sender;
this->pressedUserStatistic(currentButton->getTag());
break;
}
default:
break;
}
});
lvLeaderboard->pushBackCustomItem(btnSingleUser);

}
lvLeaderboard->setItemsMargin(0);
lvLeaderboard->setGravity(ListView::Gravity::CENTER_HORIZONTAL);
lvLeaderboard->setSize(Size(winSize.width, _height));
lvLeaderboard->setPosition(Point(0, 0));
listContainer->addChild(lvLeaderboard);

一切正常,我可以滚动排行榜,并查看我点击的每个用户的统计信息。
问题
但是有一个问题。当用户到达列表末尾时,我需要加载下一部分数据(我一次下载 50 行的结果)。但是我找不到在用户到达 ListView 的第一个或最后一个元素时起作用的变量、方法或处理程序。
我试过
ui:ListView继承ScrollView ,所以我试图在两者中找到一些方法。尽我所能,不让我想要的结果。
问题
如何确定用户何时到达 ListView 中的第一个和最后一个元素?或者,如果这不可能,如何确定 ScrollView 的结束和开始?

最佳答案

您可以安排一个更新方法或运行一个永远重复的操作来连续调用一个方法来检查 ListView 的底部项目是否有最后一个标签(在您的情况下为 tag-10 == linesCount-1)。您需要全局引用 lvLeaderboard 才能访问以下功能。

static Vec2 calculateItemPositionWithAnchor(Widget* item, const Vec2& itemAnchorPoint)
{
Vec2 origin(item->getLeftBoundary(), item->getBottomBoundary());
Size size = item->getContentSize();
return origin + Vec2(size.width * itemAnchorPoint.x, size.height * itemAnchorPoint.y);
}

static Widget* findClosestItem(const Vec2& targetPosition, const Vector<Widget*>& items, const Vec2& itemAnchorPoint, ssize_t firstIndex, float distanceFromFirst, ssize_t lastIndex, float distanceFromLast)
{
CCASSERT(firstIndex >= 0 && lastIndex < items.size() && firstIndex <= lastIndex, "");
if (firstIndex == lastIndex)
return items.at(firstIndex);

if (lastIndex - firstIndex == 1)
{
if (distanceFromFirst <= distanceFromLast)
return items.at(firstIndex);
else
return items.at(lastIndex);
}

// Binary search
ssize_t midIndex = (firstIndex + lastIndex) / 2;
Vec2 itemPosition = calculateItemPositionWithAnchor(items.at(midIndex), itemAnchorPoint);
float distanceFromMid = (targetPosition - itemPosition).length();
if (distanceFromFirst <= distanceFromLast)
return findClosestItem(targetPosition, items, itemAnchorPoint, firstIndex, distanceFromFirst, midIndex, distanceFromMid);
else
return findClosestItem(targetPosition, items, itemAnchorPoint, midIndex, distanceFromMid, lastIndex, distanceFromLast);
}

static Widget* getBottommostItemInCurrentView(ListView* lvLeaderboard)
{
const Vec2& positionRatioInView = Vec2::ANCHOR_MIDDLE_BOTTOM;
const Vec2& itemAnchorPoint = Vec2::ANCHOR_MIDDLE;

// Calculate the target position
Size contentSize = lvLeaderboard->getContentSize();
Vec2 targetPosition = -lvLeaderboard->getInnerContainerPosition();
targetPosition.x += contentSize.width * positionRatioInView.x;
targetPosition.y += contentSize.height * positionRatioInView.y;

// Find the closest item through binary search
ssize_t firstIndex = 0;
Vec2 firstPosition = calculateItemPositionWithAnchor(lvLeaderboard->getItems().at(firstIndex), itemAnchorPoint);
float distanceFromFirst = (targetPosition - firstPosition).length();

ssize_t lastIndex = lvLeaderboard->getItems().size() - 1;
Vec2 lastPosition = calculateItemPositionWithAnchor(lvLeaderboard->getItems().at(lastIndex), itemAnchorPoint);
float distanceFromLast = (targetPosition - lastPosition).length();

return findClosestItem(targetPosition, lvLeaderboard->getItems(), itemAnchorPoint, firstIndex, distanceFromFirst, lastIndex, distanceFromLast);
}

void HelloWorld::checkIfAtEnd() {

Button* bottom = (Button*)getBottommostItemInCurrentView(lvLeaderboard);

CCLOG("Bottom Button Having = %d", bottom->getTag());
if (bottom->getTag()-10 == linesCount-1) {
CCLOG("Bottom item reached..");
}
}

以下代码片段会在指定秒数(此处为 0.5 秒)后连续调用上述函数。

DelayTime* dl = DelayTime::create(0.5);
CallFunc* cb = CallFunc::create(CC_CALLBACK_0(HelloWorld::checkIfAtEnd, this));
Sequence* seq = Sequence::create(dl, cb, nullptr);
this->runAction(RepeatForever::create(seq));

关于c++ - Cocos2d-x中如何判断ScrollView结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34547643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com