weiwen
weiwen

Reputation: 466

Any ways to detect Cocos2d-x: CCScrollView is scrolling or not?

I'm seeking a way to get the scrolling state of CCScrollView.

It seems not to be a rare requirement, but do I need to implement it? Thanks:)


Edit: Following is my two 'brute force' ways, but they seems work.

The goal is to get the scroll condition of a CCScrollView s from a cocos2d::Layer l.


Way #1

In each iteration of update() function of l, get the content offset of s by

ScrollView::getContentOffset()

if it stays the same, we can assume that the ScrollView is not scrolling.


Way #2

Create class S which inherits CCScrollView and CCScrollViewDelegate, then in the override of the delegate's function

void scrollViewDidScroll(ScrollView* view)

(which seems to be called every time the ScrollView scrolls.) use a variable to save current time

/*uint64_t*/ lastScrollTime = mach_absolute_time();

then in the update() function of l, assume the ScrollView is not scrolling by a time threshold

curTime = mach_absolute_time();
if (GlobalUtils::machTimeToSecs(curTime - lastScrollTime) > 0.1)

Hope that works :)

Upvotes: 0

Views: 1462

Answers (2)

GaloisPlusPlus
GaloisPlusPlus

Reputation: 403

One way without modifying the source code of CCScrollView is to check the flag of ScrollView::isDragging() and whether one of the scrolling selectors of ScrollView is scheduled.

bool isDeaccelerateScrolling = scrollView->getScheduler()->isScheduled(CC_SCHEDULE_SELECTOR(ScrollView::deaccelerateScrolling));
bool isPerformedAnimatedScroll = scrollView->getScheduler()->isScheduled(CC_SCHEDULE_SELECTOR(ScrollView::performedAnimatedScroll));
bool isScrolling = scrollView->isDragging() || isDeaccelerateScrolling || isPerformedAnimatedScroll;

Upvotes: 1

alc77
alc77

Reputation: 409

So, in your's initScroll() method you should set:

scrollView = ScrollView::create();
// initialisation scrollview
scrollView->addEventListener(CC_CALLBACK_2(YourScene::testScroll, this));
this->addChild(scrollView);

and make bool variable isScrolled. Then in method testScroll() you need to check listener's event type and depending from it set the variable isScrolled:

void YourScene::testScroll(Ref* sender, ui::ScrollView::EventType type)
{
     if (type == ui::ScrollView::EventType::SCROLLING)
        isScrolling = true;
    else
        isScrolling = false;
}

You can see other values EventType here

Upvotes: 1

Related Questions