From c0beaa17a5ffc1e2d9cb09343ab85b93d682d454 Mon Sep 17 00:00:00 2001 From: liqiang Date: Tue, 20 Jul 2021 17:14:37 +0800 Subject: [PATCH] fix SetCurrentPage animator bug Change-Id: I24b742487a503a56cba8d127fee4520c701f609c --- frameworks/components/ui_swipe_view.cpp | 40 +++++++---- interfaces/kits/components/ui_swipe_view.h | 1 + .../ui_test_ui_swipe_view.cpp | 69 +++++++++++++++++++ .../ui_test_ui_swipe_view.h | 5 +- 4 files changed, 99 insertions(+), 16 deletions(-) diff --git a/frameworks/components/ui_swipe_view.cpp b/frameworks/components/ui_swipe_view.cpp index 5b7693a..89059c4 100755 --- a/frameworks/components/ui_swipe_view.cpp +++ b/frameworks/components/ui_swipe_view.cpp @@ -75,8 +75,32 @@ void UISwipeView::Remove(UIView* view) Invalidate(); } +void UISwipeView::MoveHeadOrTailChild() +{ + if (loop_) { + if (direction_ == HORIZONTAL) { + while (childrenHead_->GetX() >= 0) { + MoveLastChildToFirst(); + } + while (childrenTail_->GetX() + childrenTail_->GetWidth() <= GetWidth()) { + MoveFirstChildToLast(); + } + } else { + while (childrenHead_->GetY() >= 0) { + MoveLastChildToFirst(); + } + while (childrenTail_->GetY() + childrenTail_->GetHeight() <= GetHeight()) { + MoveFirstChildToLast(); + } + } + } +} + void UISwipeView::SetCurrentPage(uint16_t index, bool needAnimator) { + if (needAnimator) { + MoveHeadOrTailChild(); + } SwitchToPage(index, needAnimator); Invalidate(); } @@ -415,21 +439,7 @@ void UISwipeView::RefreshCurrentView(int16_t distance) void UISwipeView::MoveChildByOffset(int16_t xOffset, int16_t yOffset) { UIViewGroup::MoveChildByOffset(xOffset, yOffset); - if (direction_ == HORIZONTAL) { - while (isNeedLoop() && (childrenHead_->GetX() > 0)) { - MoveLastChildToFirst(); - } - while (isNeedLoop() && (childrenTail_->GetX() + childrenTail_->GetWidth() < GetWidth())) { - MoveFirstChildToLast(); - } - } else { - while (isNeedLoop() && (childrenHead_->GetY() > 0)) { - MoveLastChildToFirst(); - } - while (isNeedLoop() && (childrenTail_->GetY() + childrenTail_->GetHeight() < GetHeight())) { - MoveFirstChildToLast(); - } - } + MoveHeadOrTailChild(); } bool UISwipeView::isNeedLoop() diff --git a/interfaces/kits/components/ui_swipe_view.h b/interfaces/kits/components/ui_swipe_view.h index 1a57aca..9db046e 100755 --- a/interfaces/kits/components/ui_swipe_view.h +++ b/interfaces/kits/components/ui_swipe_view.h @@ -329,6 +329,7 @@ protected: void StopAnimator() override; virtual void SwitchToPage(int16_t dst, bool needAnimator = true); void MoveChildByOffset(int16_t xOffset, int16_t yOffset) override; + void MoveHeadOrTailChild(); /** * @brief Indicates that the animation duration is 12 ticks. diff --git a/test/uitest/test_ui_swipe_view/ui_test_ui_swipe_view.cpp b/test/uitest/test_ui_swipe_view/ui_test_ui_swipe_view.cpp index d891a04..195b68f 100755 --- a/test/uitest/test_ui_swipe_view/ui_test_ui_swipe_view.cpp +++ b/test/uitest/test_ui_swipe_view/ui_test_ui_swipe_view.cpp @@ -31,6 +31,7 @@ static int16_t g_swipeW = 400; static int16_t g_swipeHorH = 110; static int16_t g_deltaCoordinateY = 19; static int16_t g_deltaCoordinateY2 = 37; +static uint16_t g_MaxIndex = 3; } // namespace void UITestUISwipeView::SetUp() @@ -53,6 +54,8 @@ void UITestUISwipeView::TearDown() removeHeadBtn_ = nullptr; removeMidBtn_ = nullptr; removeAllBtn_ = nullptr; + loopBtn_ = nullptr; + changePageBtn_ = nullptr; lastX_ = 0; lastY_ = 0; } @@ -70,6 +73,8 @@ const UIView* UITestUISwipeView::GetTestView() UIKit_Swipe_View_Test_Ver_002(); UIKit_Swipe_View_Test_Remove_001(); + UIKit_Swipe_View_Test_SetCurrentPage(); + return container_; } @@ -344,6 +349,55 @@ void UITestUISwipeView::UIKit_Swipe_View_Test_Align_001(UISwipeView::AlignMode a SetLastPos(swipe); } +void UITestUISwipeView::UIKit_Swipe_View_Test_SetCurrentPage() +{ + if (container_ == nullptr) { + return; + } + positionY_ += g_deltaCoordinateY; + UILabel* label = GetTitleLabel("UISwipeView切换页面"); + container_->Add(label); + positionY_ += g_deltaCoordinateY; + label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_); + positionY_ += g_deltaCoordinateY2; + + UISwipeView* swipe = new UISwipeView(UISwipeView::HORIZONTAL); + swipe->SetIntercept(true); + swipe->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full); + swipe->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_, g_swipeW, g_swipeH); + swipe->SetLoopState(loop_); + swipe->SetAnimatorTime(1000); // 100: mean animator drag time(ms) + currentSwipe_ = swipe; + container_->Add(swipe); + UIView* view1 = new UIView(); + view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full); + view1->Resize(g_swipeW, g_swipeH); + swipe->Add(view1); + UIView* view2 = new UIView(); + view2->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full); + view2->Resize(g_swipeW, g_swipeH); + swipe->Add(view2); + UIView* view3 = new UIView(); + view3->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full); + view3->Resize(g_swipeW, g_swipeH); + swipe->Add(view3); + UIView* view4 = new UIView(); + view4->SetStyle(STYLE_BACKGROUND_COLOR, Color::Yellow().full); + view4->Resize(g_swipeW, g_swipeH); + swipe->Add(view4); + if (loopBtn_ == nullptr) { + loopBtn_ = new UILabelButton(); + } + if (changePageBtn_ == nullptr) { + changePageBtn_ = new UILabelButton(); + } + + positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE + swipe->GetWidth() + 20; // 20: is interval between button and swipe + positionY_ += g_deltaCoordinateY2; + SetUpButton(loopBtn_, "设置循环 关 "); + SetUpButton(changePageBtn_, "切换页面 "); +} + bool UITestUISwipeView::OnClick(UIView& view, const ClickEvent& event) { if (currentSwipe_ == nullptr) { @@ -371,6 +425,21 @@ bool UITestUISwipeView::OnClick(UIView& view, const ClickEvent& event) currentSwipe_->Remove(view); } else if (&view == removeAllBtn_) { currentSwipe_->RemoveAll(); + } else if (&view == loopBtn_) { + loop_ = !loop_; + currentSwipe_->SetLoopState(loop_); + if (!loop_) { + loopBtn_->SetText("设置循环 关 "); + } else { + loopBtn_->SetText("设置循环 开 "); + } + } else if (&view == changePageBtn_) { + uint16_t currentIndex = currentSwipe_->GetCurrentPage(); + if (currentIndex < g_MaxIndex) { + currentSwipe_->SetCurrentPage(++currentIndex, true); + } else { + currentSwipe_->SetCurrentPage(0, true); + } } currentSwipe_->Invalidate(); btnNum_++; diff --git a/test/uitest/test_ui_swipe_view/ui_test_ui_swipe_view.h b/test/uitest/test_ui_swipe_view/ui_test_ui_swipe_view.h index 8718590..6198338 100755 --- a/test/uitest/test_ui_swipe_view/ui_test_ui_swipe_view.h +++ b/test/uitest/test_ui_swipe_view/ui_test_ui_swipe_view.h @@ -38,6 +38,7 @@ public: void UIKit_Swipe_View_Test_Ver_002(); void UIKit_Swipe_View_Test_Remove_001(); void UIKit_Swipe_View_Test_Align_001(UISwipeView::AlignMode alignMode); + void UIKit_Swipe_View_Test_SetCurrentPage(); private: void SetUpButton(UILabelButton* btn, const char* title); @@ -50,7 +51,9 @@ private: UILabelButton* removeHeadBtn_ = nullptr; UILabelButton* removeMidBtn_ = nullptr; UILabelButton* removeAllBtn_ = nullptr; - + UILabelButton* loopBtn_ = nullptr; + UILabelButton* changePageBtn_ = nullptr; + bool loop_ = false; int16_t lastX_ = 0; int16_t lastY_ = 0; int16_t btnNum_ = 0; -- GitLab