diff --git a/cocos/ui/UIPageView.cpp b/cocos/ui/UIPageView.cpp index 8317b3d989..6d2677e8a0 100644 --- a/cocos/ui/UIPageView.cpp +++ b/cocos/ui/UIPageView.cpp @@ -43,7 +43,7 @@ _rightBoundary(0.0f), _isAutoScrolling(false), _autoScrollDistance(0.0f), _autoScrollSpeed(0.0f), -_autoScrollDir(0), +_autoScrollDirection(AutoScrollDirection::LEFT), _childFocusCancelOffset(5.0f), _pageViewEventListener(nullptr), _pageViewEventSelector(nullptr), @@ -262,7 +262,7 @@ void PageView::scrollToPage(ssize_t idx) Layout* curPage = _pages.at(idx); _autoScrollDistance = -(curPage->getPosition().x); _autoScrollSpeed = fabs(_autoScrollDistance)/0.2f; - _autoScrollDir = _autoScrollDistance > 0 ? 1 : 0; + _autoScrollDirection = _autoScrollDistance > 0 ? AutoScrollDirection::RIGHT : AutoScrollDirection::LEFT; _isAutoScrolling = true; } @@ -270,9 +270,15 @@ void PageView::update(float dt) { if (_isAutoScrolling) { - switch (_autoScrollDir) + this->autoScroll(dt); + } +} + +void PageView::autoScroll(float dt) + { + switch (_autoScrollDirection) { - case 0: + case AutoScrollDirection::LEFT: { float step = _autoScrollSpeed*dt; if (_autoScrollDistance + step >= 0.0f) @@ -293,7 +299,7 @@ void PageView::update(float dt) break; } break; - case 1: + case AutoScrollDirection::RIGHT: { float step = _autoScrollSpeed*dt; if (_autoScrollDistance - step <= 0.0f) @@ -307,17 +313,19 @@ void PageView::update(float dt) _autoScrollDistance -= step; } scrollPages(step); + if (!_isAutoScrolling) { pageTurningEvent(); } + break; } default: break; } + } -} bool PageView::onTouchBegan(Touch *touch, Event *unusedEvent) { @@ -576,7 +584,7 @@ void PageView::copyClonedWidgetChildren(Widget* model) auto modelPages = static_cast(model)->getPages(); for (auto& page : modelPages) { - addPage(dynamic_cast(page->clone())); + addPage(static_cast(page->clone())); } } diff --git a/cocos/ui/UIPageView.h b/cocos/ui/UIPageView.h index b0da7cd06d..b2d7bf8852 100644 --- a/cocos/ui/UIPageView.h +++ b/cocos/ui/UIPageView.h @@ -187,6 +187,7 @@ protected: void pageTurningEvent(); void updateAllPagesSize(); void updateAllPagesPosition(); + void autoScroll(float dt); virtual void handlePressLogic(const Vec2 &touchPoint); virtual void handleMoveLogic(const Vec2 &touchPoint) ; @@ -203,6 +204,16 @@ protected: virtual void doLayout() override; protected: + enum class AutoScrollDirection + { + LEFT, + RIGHT + }; + bool _isAutoScrolling; + float _autoScrollDistance; + float _autoScrollSpeed; + AutoScrollDirection _autoScrollDirection; + ssize_t _curPageIdx; Vector _pages; @@ -215,10 +226,7 @@ protected: Widget* _rightBoundaryChild; float _leftBoundary; float _rightBoundary; - bool _isAutoScrolling; - float _autoScrollDistance; - float _autoScrollSpeed; - int _autoScrollDir; + float _childFocusCancelOffset;