fix pageview turning event (#16299)

This commit is contained in:
子龙山人 2016-08-04 09:51:54 +08:00 committed by minggo
parent ad721ec3a4
commit 6eaf15a2c2
4 changed files with 38 additions and 15 deletions

View File

@ -39,7 +39,8 @@ _childFocusCancelOffset(5.0f),
_pageViewEventListener(nullptr), _pageViewEventListener(nullptr),
_pageViewEventSelector(nullptr), _pageViewEventSelector(nullptr),
_eventCallback(nullptr), _eventCallback(nullptr),
_autoScrollStopEpsilon(0.001f) _autoScrollStopEpsilon(0.001f),
_previousPageIndex(-1)
{ {
} }
@ -219,6 +220,12 @@ void PageView::refreshIndicatorPosition()
} }
} }
void PageView::handlePressLogic(Touch *touch)
{
ListView::handlePressLogic(touch);
_previousPageIndex = _currentPageIndex;
}
void PageView::handleReleaseLogic(Touch *touch) void PageView::handleReleaseLogic(Touch *touch)
{ {
// Use `ScrollView` method in order to avoid `startMagneticScroll()` by `ListView`. // Use `ScrollView` method in order to avoid `startMagneticScroll()` by `ListView`.
@ -228,7 +235,6 @@ void PageView::handleReleaseLogic(Touch *touch)
{ {
return; return;
} }
Vec2 touchMoveVelocity = flattenVectorByDirection(calculateTouchMoveVelocity()); Vec2 touchMoveVelocity = flattenVectorByDirection(calculateTouchMoveVelocity());
static const float INERTIA_THRESHOLD = 500; static const float INERTIA_THRESHOLD = 500;
@ -272,6 +278,19 @@ float PageView::getAutoScrollStopEpsilon() const
return _autoScrollStopEpsilon; return _autoScrollStopEpsilon;
} }
void PageView::addEventListenerPageView(Ref *target, SEL_PageViewEvent selector)
{
_pageViewEventListener = target;
_pageViewEventSelector = selector;
ccScrollViewCallback scrollViewCallback = [=](Ref* ref, ScrollView::EventType type) -> void{
if (type == ScrollView::EventType::AUTOSCROLL_ENDED && _previousPageIndex != _currentPageIndex) {
pageTurningEvent();
}
};
this->addEventListener(scrollViewCallback);
}
void PageView::pageTurningEvent() void PageView::pageTurningEvent()
{ {
this->retain(); this->retain();
@ -290,18 +309,12 @@ void PageView::pageTurningEvent()
this->release(); this->release();
} }
void PageView::addEventListenerPageView(Ref *target, SEL_PageViewEvent selector)
{
_pageViewEventListener = target;
_pageViewEventSelector = selector;
}
void PageView::addEventListener(const ccPageViewCallback& callback) void PageView::addEventListener(const ccPageViewCallback& callback)
{ {
_eventCallback = callback; _eventCallback = callback;
ccScrollViewCallback scrollViewCallback = [=](Ref* ref, ScrollView::EventType type) -> void{ ccScrollViewCallback scrollViewCallback = [=](Ref* ref, ScrollView::EventType type) -> void{
if (type == ScrollView::EventType::AUTOSCROLL_ENDED) { if (type == ScrollView::EventType::AUTOSCROLL_ENDED && _previousPageIndex != _currentPageIndex) {
callback(ref, PageView::EventType::TURNING); pageTurningEvent();
} }
}; };
this->addEventListener(scrollViewCallback); this->addEventListener(scrollViewCallback);
@ -364,6 +377,11 @@ void PageView::copySpecialProperties(Widget *widget)
_ccEventCallback = pageView->_ccEventCallback; _ccEventCallback = pageView->_ccEventCallback;
_pageViewEventListener = pageView->_pageViewEventListener; _pageViewEventListener = pageView->_pageViewEventListener;
_pageViewEventSelector = pageView->_pageViewEventSelector; _pageViewEventSelector = pageView->_pageViewEventSelector;
_currentPageIndex = pageView->_currentPageIndex;
_previousPageIndex = pageView->_previousPageIndex;
_childFocusCancelOffset = pageView->_childFocusCancelOffset;
_autoScrollStopEpsilon = pageView->_autoScrollStopEpsilon;
_indicatorPositionAsAnchorPoint = pageView->_indicatorPositionAsAnchorPoint;
} }
} }

View File

@ -394,6 +394,7 @@ protected:
virtual void onItemListChanged() override; virtual void onItemListChanged() override;
virtual void onSizeChanged() override; virtual void onSizeChanged() override;
virtual void handleReleaseLogic(Touch *touch) override; virtual void handleReleaseLogic(Touch *touch) override;
virtual void handlePressLogic(Touch *touch) override;
virtual Widget* createCloneInstance() override; virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override; virtual void copySpecialProperties(Widget* model) override;
@ -423,6 +424,7 @@ protected:
#endif #endif
ccPageViewCallback _eventCallback; ccPageViewCallback _eventCallback;
float _autoScrollStopEpsilon; float _autoScrollStopEpsilon;
ssize_t _previousPageIndex;
}; };
} }

View File

@ -84,7 +84,8 @@ bool UIPageViewTest::init()
pageView->removeItem(0); pageView->removeItem(0);
pageView->scrollToItem(pageCount - 2); pageView->scrollToItem(pageCount - 2);
pageView->addEventListener((PageView::ccPageViewCallback)CC_CALLBACK_2(UIPageViewTest::pageViewEvent, this)); //This method is deprecated, we used here only testing purpose
pageView->addEventListenerPageView(this, pagevieweventselector(UIPageViewTest::pageViewEvent));
_uiLayer->addChild(pageView); _uiLayer->addChild(pageView);
@ -93,11 +94,11 @@ bool UIPageViewTest::init()
return false; return false;
} }
void UIPageViewTest::pageViewEvent(Ref *pSender, PageView::EventType type) void UIPageViewTest::pageViewEvent(Ref *pSender, PageViewEventType type)
{ {
switch (type) switch (type)
{ {
case PageView::EventType::TURNING: case PAGEVIEW_EVENT_TURNING:
{ {
PageView* pageView = dynamic_cast<PageView*>(pSender); PageView* pageView = dynamic_cast<PageView*>(pSender);
@ -199,7 +200,9 @@ bool UIPageViewButtonTest::init()
void UIPageViewButtonTest::onButtonClicked(Ref* sender, Widget::TouchEventType type) void UIPageViewButtonTest::onButtonClicked(Ref* sender, Widget::TouchEventType type)
{ {
if(type == Widget::TouchEventType::ENDED) {
log("button %s clicked", static_cast<Button*>(sender)->getName().c_str()); log("button %s clicked", static_cast<Button*>(sender)->getName().c_str());
}
} }

View File

@ -38,7 +38,7 @@ public:
~UIPageViewTest(); ~UIPageViewTest();
virtual bool init() override; virtual bool init() override;
void pageViewEvent(cocos2d::Ref* sender, cocos2d::ui::PageView::EventType type); void pageViewEvent(cocos2d::Ref* sender, cocos2d::ui::PageViewEventType type);
protected: protected: