@@ -15,6 +15,7 @@ UIScrollViewTests::UIScrollViewTests()
15
15
ADD_TEST_CASE (UIScrollViewDisableTest);
16
16
ADD_TEST_CASE (UIScrollViewInnerSize);
17
17
ADD_TEST_CASE (UIScrollViewTestEvents);
18
+ ADD_TEST_CASE (UIScrollViewStopScrollingTest);
18
19
}
19
20
// UIScrollViewTest_Vertical
20
21
@@ -684,8 +685,10 @@ bool UIScrollViewInnerSize::init()
684
685
return false ;
685
686
}
686
687
688
+ // UIScrollViewTestEvents
689
+
687
690
UIScrollViewTestEvents::UIScrollViewTestEvents ()
688
- : _displayValueLabel(nullptr )
691
+ : _displayValueLabel(nullptr )
689
692
{
690
693
691
694
}
@@ -762,3 +765,131 @@ bool UIScrollViewTestEvents::init()
762
765
return false ;
763
766
}
764
767
768
+ // UIScrollViewStopScrollingTest
769
+
770
+ UIScrollViewStopScrollingTest::UIScrollViewStopScrollingTest ()
771
+ : _displayValueLabel(nullptr )
772
+ , _scrollView(nullptr )
773
+ , _remainingTime(0 .0f )
774
+ {
775
+
776
+ }
777
+
778
+ bool UIScrollViewStopScrollingTest::init ()
779
+ {
780
+ if (UIScene::init ())
781
+ {
782
+ Size widgetSize = _widget->getContentSize ();
783
+
784
+ // Add a label in which the time remaining till scrolling stop will be displayed.
785
+ _displayValueLabel = Text::create (" Scrolling stop isn't scheduled" , " fonts/Marker Felt.ttf" , 32 );
786
+ _displayValueLabel->setAnchorPoint (Vec2 (0 .5f , -1 .0f ));
787
+ _displayValueLabel->setPosition (Vec2 (widgetSize.width / 2 .0f , widgetSize.height / 2 .0f + _displayValueLabel->getContentSize ().height * 1 .5f ));
788
+ _uiLayer->addChild (_displayValueLabel);
789
+
790
+ // Add the alert
791
+ Text* alert = Text::create (" Click the button and start to scroll" , " fonts/Marker Felt.ttf" , 30 );
792
+ alert->setColor (Color3B (159 , 168 , 176 ));
793
+ alert->setPosition (Vec2 (widgetSize.width / 2 .0f , widgetSize.height / 2 .0f - alert->getContentSize ().height * 3 .075f ));
794
+ _uiLayer->addChild (alert);
795
+
796
+ Layout* root = static_cast <Layout*>(_uiLayer->getChildByTag (81 ));
797
+
798
+ Layout* background = static_cast <Layout*>(root->getChildByName (" background_Panel" ));
799
+
800
+ // Create the dragpanel
801
+ _scrollView = ui::ScrollView::create ();
802
+ _scrollView->setDirection (ui::ScrollView::Direction::BOTH);
803
+ _scrollView->setTouchEnabled (true );
804
+ _scrollView->setBounceEnabled (true );
805
+ _scrollView->setBackGroundImageScale9Enabled (true );
806
+ _scrollView->setBackGroundImage (" cocosui/green_edit.png" );
807
+ _scrollView->setContentSize (Size (210 , 122.5 ));
808
+ _scrollView->setScrollBarWidth (4 );
809
+ _scrollView->setScrollBarPositionFromCorner (Vec2 (6 , 6 ));
810
+ Size backgroundSize = background->getContentSize ();
811
+ _scrollView->setPosition (Vec2 ((widgetSize.width - backgroundSize.width ) / 2 .0f +
812
+ (backgroundSize.width - _scrollView->getContentSize ().width ) / 2 .0f ,
813
+ (widgetSize.height - backgroundSize.height ) / 2 .0f +
814
+ (backgroundSize.height - _scrollView->getContentSize ().height ) / 2 .0f ));
815
+ ImageView* imageView = ImageView::create (" Hello.png" );
816
+ _scrollView->addChild (imageView);
817
+ _scrollView->setInnerContainerSize (imageView->getContentSize ());
818
+ Size innerSize = _scrollView->getInnerContainerSize ();
819
+ imageView->setPosition (Vec2 (innerSize.width / 2 .0f , innerSize.height / 2 .0f ));
820
+ _uiLayer->addChild (_scrollView);
821
+
822
+ // Log some ScrollView events.
823
+ _scrollView->addEventListener ([&] (Ref*, ui::ScrollView::EventType e)
824
+ {
825
+ switch (e)
826
+ {
827
+ case ui::ScrollView::EventType::SCROLLING_BEGAN:
828
+ CCLOG (" scrolling began!" );
829
+ break ;
830
+ case ui::ScrollView::EventType::SCROLLING_ENDED:
831
+ CCLOG (" scrolling ended!" );
832
+ break ;
833
+ case ui::ScrollView::EventType::AUTOSCROLL_ENDED:
834
+ CCLOG (" auto-scrolling ended!" );
835
+ break ;
836
+ default : break ;
837
+ }
838
+ });
839
+
840
+ // Jump to right bottom
841
+ _scrollView->jumpToBottomRight ();
842
+
843
+ // Add button that will schedule scrolling stop when it is clicked.
844
+ Button* button_scale9 = Button::create (" cocosui/button.png" , " cocosui/buttonHighlighted.png" );
845
+ button_scale9->setTitleText (" Stop scrolling in 3 sec." );
846
+ button_scale9->setScale9Enabled (true );
847
+ button_scale9->setContentSize (Size (120 .0f , button_scale9->getVirtualRendererSize ().height ));
848
+ button_scale9->setPosition (Vec2 (innerSize.width / 2 .0f , innerSize.height / 2 .0f ));
849
+ button_scale9->addClickEventListener ([this ] (Ref*) { this ->_remainingTime = 3 .0f ; });
850
+ _scrollView->addChild (button_scale9);
851
+
852
+ // Schedule update for this scene.
853
+ Director::getInstance ()->getScheduler ()->scheduleUpdate (this , 0 , false );
854
+
855
+ return true ;
856
+ }
857
+
858
+ return false ;
859
+ }
860
+
861
+ void UIScrollViewStopScrollingTest::update (float dt)
862
+ {
863
+ UIScene::update (dt);
864
+
865
+ if (_remainingTime > 0 .0f )
866
+ {
867
+ _remainingTime -= dt;
868
+
869
+ if (_remainingTime > 0 .0f )
870
+ {
871
+ // Update timer caption.
872
+ char strRemainingTime[100 ];
873
+ sprintf (strRemainingTime, " Stop scrolling in %.1f sec." , _remainingTime);
874
+ _displayValueLabel->setString (std::string (strRemainingTime));
875
+ }
876
+ else
877
+ {
878
+ _scrollView->stopOverallScroll ();
879
+
880
+ // Update timer caption.
881
+ std::string strRemainingTime = " Scrolling stop isn't scheduled" ;
882
+ _displayValueLabel->setString (strRemainingTime);
883
+
884
+ // Show hint label.
885
+ auto hintLabel = Label::createWithTTF (" Stopped!" , " fonts/Marker Felt.ttf" , 32 .0f );
886
+ Size contentSize = _uiLayer->getContentSize ();
887
+ hintLabel->setPosition (Vec2 (contentSize.width / 2 .0f , contentSize.height / 2 .0f ));
888
+ hintLabel->runAction (Spawn::createWithTwoActions (
889
+ FadeOut::create (0 .3f ),
890
+ ScaleTo::create (0 .3f , 2 .0f )
891
+ ));
892
+ _uiLayer->addChild (hintLabel);
893
+ }
894
+ }
895
+ }
0 commit comments