issue #5176, refactor AutoScrollDirection in UIPageView

This commit is contained in:
andyque 2014-05-22 14:09:01 +08:00
parent 0c065b4be0
commit 2344928945
2 changed files with 27 additions and 11 deletions

View File

@ -43,7 +43,7 @@ _rightBoundary(0.0f),
_isAutoScrolling(false), _isAutoScrolling(false),
_autoScrollDistance(0.0f), _autoScrollDistance(0.0f),
_autoScrollSpeed(0.0f), _autoScrollSpeed(0.0f),
_autoScrollDir(0), _autoScrollDirection(AutoScrollDirection::LEFT),
_childFocusCancelOffset(5.0f), _childFocusCancelOffset(5.0f),
_pageViewEventListener(nullptr), _pageViewEventListener(nullptr),
_pageViewEventSelector(nullptr), _pageViewEventSelector(nullptr),
@ -262,7 +262,7 @@ void PageView::scrollToPage(ssize_t idx)
Layout* curPage = _pages.at(idx); Layout* curPage = _pages.at(idx);
_autoScrollDistance = -(curPage->getPosition().x); _autoScrollDistance = -(curPage->getPosition().x);
_autoScrollSpeed = fabs(_autoScrollDistance)/0.2f; _autoScrollSpeed = fabs(_autoScrollDistance)/0.2f;
_autoScrollDir = _autoScrollDistance > 0 ? 1 : 0; _autoScrollDirection = _autoScrollDistance > 0 ? AutoScrollDirection::RIGHT : AutoScrollDirection::LEFT;
_isAutoScrolling = true; _isAutoScrolling = true;
} }
@ -270,9 +270,15 @@ void PageView::update(float dt)
{ {
if (_isAutoScrolling) if (_isAutoScrolling)
{ {
switch (_autoScrollDir) this->autoScroll(dt);
}
}
void PageView::autoScroll(float dt)
{
switch (_autoScrollDirection)
{ {
case 0: case AutoScrollDirection::LEFT:
{ {
float step = _autoScrollSpeed*dt; float step = _autoScrollSpeed*dt;
if (_autoScrollDistance + step >= 0.0f) if (_autoScrollDistance + step >= 0.0f)
@ -293,7 +299,7 @@ void PageView::update(float dt)
break; break;
} }
break; break;
case 1: case AutoScrollDirection::RIGHT:
{ {
float step = _autoScrollSpeed*dt; float step = _autoScrollSpeed*dt;
if (_autoScrollDistance - step <= 0.0f) if (_autoScrollDistance - step <= 0.0f)
@ -307,17 +313,19 @@ void PageView::update(float dt)
_autoScrollDistance -= step; _autoScrollDistance -= step;
} }
scrollPages(step); scrollPages(step);
if (!_isAutoScrolling) if (!_isAutoScrolling)
{ {
pageTurningEvent(); pageTurningEvent();
} }
break; break;
} }
default: default:
break; break;
} }
} }
}
bool PageView::onTouchBegan(Touch *touch, Event *unusedEvent) bool PageView::onTouchBegan(Touch *touch, Event *unusedEvent)
{ {
@ -576,7 +584,7 @@ void PageView::copyClonedWidgetChildren(Widget* model)
auto modelPages = static_cast<PageView*>(model)->getPages(); auto modelPages = static_cast<PageView*>(model)->getPages();
for (auto& page : modelPages) for (auto& page : modelPages)
{ {
addPage(dynamic_cast<Layout*>(page->clone())); addPage(static_cast<Layout*>(page->clone()));
} }
} }

View File

@ -187,6 +187,7 @@ protected:
void pageTurningEvent(); void pageTurningEvent();
void updateAllPagesSize(); void updateAllPagesSize();
void updateAllPagesPosition(); void updateAllPagesPosition();
void autoScroll(float dt);
virtual void handlePressLogic(const Vec2 &touchPoint); virtual void handlePressLogic(const Vec2 &touchPoint);
virtual void handleMoveLogic(const Vec2 &touchPoint) ; virtual void handleMoveLogic(const Vec2 &touchPoint) ;
@ -203,6 +204,16 @@ protected:
virtual void doLayout() override; virtual void doLayout() override;
protected: protected:
enum class AutoScrollDirection
{
LEFT,
RIGHT
};
bool _isAutoScrolling;
float _autoScrollDistance;
float _autoScrollSpeed;
AutoScrollDirection _autoScrollDirection;
ssize_t _curPageIdx; ssize_t _curPageIdx;
Vector<Layout*> _pages; Vector<Layout*> _pages;
@ -215,10 +226,7 @@ protected:
Widget* _rightBoundaryChild; Widget* _rightBoundaryChild;
float _leftBoundary; float _leftBoundary;
float _rightBoundary; float _rightBoundary;
bool _isAutoScrolling;
float _autoScrollDistance;
float _autoScrollSpeed;
int _autoScrollDir;
float _childFocusCancelOffset; float _childFocusCancelOffset;