如何在Cocos2d-x中确定ScrollView的结束

How to determine the end of ScrollView in Cocos2d-x?

本文关键字:ScrollView 结束 Cocos2d-x      更新时间:2023-10-16

上下文
我通过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的结束和开始?

您可以安排一个更新方法或运行一个永远重复的操作来连续调用一个方法,以检查列表视图的底部项是否有最后一个标记(在您的情况下是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));

您可以将eventListener添加到您的滚动视图中,然后像这样进行检测和检查

scrollView->addEventListener([](Ref *r, ScrollView::EventType type) {
  if (type == ScrollView::EventType::SCROLL_TO_BOTTOM) {// SCROLL_TO_TOP
    // do what you need to do 
  }
});