ScrollView
scrollView->setContentSize(Size(
280.0f
,
100.0f
));
Boundary
/'baʊnd(ə)rɪ/
分界线,
界线, 范围
inner
/ɪnə/
内部的,
里面的
trigger
/'trɪgə/
触发,
引发,
松开扳柄
n. 触发器;扳机;
void
scrollToBottom(
float
time,
bool
attenuated);
//
attenuated是否伴有从快到慢的减速效果
//
在指定时间内,滚动容器内容到滚动视图底部
void
scrollToPercentVertical(
float
percent,
float
time,
bool
attenuated);//竖直滚动容器内容到滚动视图高度百分比位置上
void
scrollToPercentHorizontal(
float
percent,
float
time,
bool
attenuated);
//水平滚动容器内容到滚动视图宽度百分比位置上
void
scrollToPercentBothDirection(
const
Vec2
& percent,
float
time,
bool
attenuated);
//两个方向同时滚动容器内容到滚动视图百分比位置上
void
jumpToBottom();
//
立即跳转到滚动视图底部
void
setInnerContainerSize(
const
Size
&size);
//设置滚动层容器的内部容器大小
void setInertiaScrollEnabled(bool enabled);
//设置是否开启滚动伴有惯性效果,默认开启
virtual
void
addEventListener(
const
ccScrollViewCallback
& callback);
enum
class
Direction
{
{
NONE,
VERTICAL,
//竖直
HORIZONTAL,
//水平
BOTH
//
竖直和
水平二者皆可
};
enum class EventType
{
SCROLL_TO_TOP,
SCROLL_TO_BOTTOM,
SCROLL_TO_LEFT,
SCROLL_TO_RIGHT,
SCROLLING,
BOUNCE_TOP,
BOUNCE_BOTTOM,
BOUNCE_LEFT,
BOUNCE_RIGHT
};
virtual
void
setDirection(
Direction
dir);
Direction
getDirection()
const
;
Layout
* getInnerContainer()
const
;
void
scrollToBottom(
float
time,
bool
attenuated);
//
attenuated是否伴有从快到慢的减速效果
//
在指定时间内,滚动容器内容到滚动视图底部
void
scrollToTop(
float
time,
bool
attenuated);
void
scrollToLeft(
float
time,
bool
attenuated);
void
scrollToRight(
float
time,
bool
attenuated);
void
scrollToTopLeft(
float
time,
bool
attenuated);
void
scrollToTopRight(
float
time,
bool
attenuated);
void
scrollToBottomLeft(
float
time,
bool
attenuated);
void
scrollToBottomRight(
float
time,
bool
attenuated);
void
scrollToPercentVertical(
float
percent,
float
time,
bool
attenuated);//竖直滚动容器内容到滚动视图高度百分比位置上
void
scrollToPercentHorizontal(
float
percent,
float
time,
bool
attenuated);
//水平滚动容器内容到滚动视图宽度百分比位置上
void
scrollToPercentBothDirection(
const
Vec2
& percent,
float
time,
bool
attenuated);
//两个方向同时滚动容器内容到滚动视图百分比位置上
void
jumpToBottom();
//
立即跳转到滚动视图底部
void
jumpToTop();
void
jumpToLeft();
void
jumpToRight();
void
jumpToTopLeft();
void
jumpToTopRight();
void
jumpToBottomLeft();
void
jumpToBottomRight();
void
jumpToPercentVertical(
float
percent);
void
jumpToPercentHorizontal(
float
percent);
void
jumpToPercentBothDirection(
const
Vec2
& percent);
void
setInnerContainerSize(
const
Size
&size);
//设置滚动层容器的内部容器大小
const
Size
& getInnerContainerSize()
const
;
/**
* Add call back function called scrollview event triggered
*/
CC_DEPRECATED_ATTRIBUTE void addEventListenerScrollView( Ref * target, SEL_ScrollViewEvent selector);
virtual void addEventListener( const ccScrollViewCallback & callback);
/**
* Add call back function called scrollview event triggered
*/
CC_DEPRECATED_ATTRIBUTE void addEventListenerScrollView( Ref * target, SEL_ScrollViewEvent selector);
virtual void addEventListener( const ccScrollViewCallback & callback);
//
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
//all of these functions are related to innerContainer.
virtual void addChild( Node * child)override;
virtual void addChild( Node * child, int localZOrder)override;
virtual void addChild( Node * child, int zOrder, int tag) override;
virtual void addChild( Node * child, int zOrder, const std :: string &name) override;
virtual void removeAllChildren() override;
virtual void removeAllChildrenWithCleanup( bool cleanup) override;
virtual void removeChild( Node * child, bool cleaup = true ) override;
virtual Vector < Node *>& getChildren() override;
virtual const Vector < Node *>& getChildren() const override;
virtual ssize_t getChildrenCount() const override;
virtual Node * getChildByTag( int tag) const override;
virtual Node * getChildByName( const std :: string & name) const override;
//handle touch event
virtual bool onTouchBegan( Touch *touch, Event *unusedEvent) override;
virtual void onTouchMoved( Touch *touch, Event *unusedEvent) override;
virtual void onTouchEnded( Touch *touch, Event *unusedEvent) override;
virtual void onTouchCancelled( Touch *touch, Event *unusedEvent) override;
virtual void update( float dt) override;
void setBounceEnabled( bool enabled);
bool isBounceEnabled() const ;
virtual void addChild( Node * child)override;
virtual void addChild( Node * child, int localZOrder)override;
virtual void addChild( Node * child, int zOrder, int tag) override;
virtual void addChild( Node * child, int zOrder, const std :: string &name) override;
virtual void removeAllChildren() override;
virtual void removeAllChildrenWithCleanup( bool cleanup) override;
virtual void removeChild( Node * child, bool cleaup = true ) override;
virtual Vector < Node *>& getChildren() override;
virtual const Vector < Node *>& getChildren() const override;
virtual ssize_t getChildrenCount() const override;
virtual Node * getChildByTag( int tag) const override;
virtual Node * getChildByName( const std :: string & name) const override;
//handle touch event
virtual bool onTouchBegan( Touch *touch, Event *unusedEvent) override;
virtual void onTouchMoved( Touch *touch, Event *unusedEvent) override;
virtual void onTouchEnded( Touch *touch, Event *unusedEvent) override;
virtual void onTouchCancelled( Touch *touch, Event *unusedEvent) override;
virtual void update( float dt) override;
void setBounceEnabled( bool enabled);
bool isBounceEnabled() const ;
void setInertiaScrollEnabled(bool enabled);
//设置是否开启滚动伴有惯性效果,默认开启
bool isInertiaScrollEnabled() const;
virtual
void
setLayoutType(
Type
type) override;
virtual
Type
getLayoutType()
const
override;
virtual
std
::
string
getDescription()
const
override;
virtual void onEnter() override;
virtual
Widget
* findNextFocusedWidget(
FocusDirection
direction,
Widget
* current) override;
CC_CONSTRUCTOR_ACCESS :
virtual bool init() override;
protected :
virtual void initRenderer() override;
virtual void onSizeChanged() override;
virtual void doLayout() override;
virtual Widget * createCloneInstance() override;
virtual void copySpecialProperties( Widget * model) override;
virtual void copyClonedWidgetChildren( Widget * model) override;
void moveChildren( float offsetX, float offsetY);
void autoScrollChildren( float dt);
void bounceChildren( float dt);
void checkBounceBoundary();
bool checkNeedBounce();
void startAutoScrollChildrenWithOriginalSpeed( const Vec2 & dir, float v, bool attenuated, float acceleration);
void startAutoScrollChildrenWithDestination( const Vec2 & des, float time, bool attenuated);
void jumpToDestination( const Vec2 & des);
void stopAutoScrollChildren();
void startBounceChildren( float v);
void stopBounceChildren();
bool checkCustomScrollDestination( float * touchOffsetX, float * touchOffsetY);
virtual bool scrollChildren( float touchOffsetX, float touchOffsetY);
bool scrollChildrenVertical( float touchOffsetX, float touchOffsetY);
bool scrollChildrenHorizontal( float touchOffsetX, float touchOffestY);
bool scrollChildrenBoth( float touchOffsetX, float touchOffsetY);
bool bounceScrollChildren( float touchOffsetX, float touchOffsetY);
void startRecordSlidAction();
virtual void endRecordSlidAction();
//ScrollViewProtocol
virtual void handlePressLogic( Touch *touch) ;
virtual void handleMoveLogic( Touch *touch) ;
virtual void handleReleaseLogic( Touch *touch) ;
virtual void interceptTouchEvent( Widget :: TouchEventType event, Widget * sender, Touch *touch) override;
void recordSlidTime( float dt);
void scrollToTopEvent();
void scrollToBottomEvent();
void scrollToLeftEvent();
void scrollToRightEvent();
void scrollingEvent();
void bounceTopEvent();
void bounceBottomEvent();
void bounceLeftEvent();
void bounceRightEvent();
protected :
Layout * _innerContainer;
Direction _direction;
Vec2 _autoScrollDir;
float _topBoundary;
float _bottomBoundary;
float _leftBoundary;
float _rightBoundary;
float _bounceTopBoundary;
float _bounceBottomBoundary;
float _bounceLeftBoundary;
float _bounceRightBoundary;
bool _autoScroll;
float _autoScrollAddUpTime;
float _autoScrollOriginalSpeed;
float _autoScrollAcceleration;
bool _isAutoScrollSpeedAttenuated;
bool _needCheckAutoScrollDestination;
Vec2 _autoScrollDestination;
bool _bePressed;
float _slidTime;
Vec2 _moveChildPoint;
float _childFocusCancelOffset;
bool _leftBounceNeeded;
bool _topBounceNeeded;
bool _rightBounceNeeded;
bool _bottomBounceNeeded;
bool _bounceEnabled;
bool _bouncing;
Vec2 _bounceDir;
float _bounceOriginalSpeed;
CC_CONSTRUCTOR_ACCESS :
virtual bool init() override;
protected :
virtual void initRenderer() override;
virtual void onSizeChanged() override;
virtual void doLayout() override;
virtual Widget * createCloneInstance() override;
virtual void copySpecialProperties( Widget * model) override;
virtual void copyClonedWidgetChildren( Widget * model) override;
void moveChildren( float offsetX, float offsetY);
void autoScrollChildren( float dt);
void bounceChildren( float dt);
void checkBounceBoundary();
bool checkNeedBounce();
void startAutoScrollChildrenWithOriginalSpeed( const Vec2 & dir, float v, bool attenuated, float acceleration);
void startAutoScrollChildrenWithDestination( const Vec2 & des, float time, bool attenuated);
void jumpToDestination( const Vec2 & des);
void stopAutoScrollChildren();
void startBounceChildren( float v);
void stopBounceChildren();
bool checkCustomScrollDestination( float * touchOffsetX, float * touchOffsetY);
virtual bool scrollChildren( float touchOffsetX, float touchOffsetY);
bool scrollChildrenVertical( float touchOffsetX, float touchOffsetY);
bool scrollChildrenHorizontal( float touchOffsetX, float touchOffestY);
bool scrollChildrenBoth( float touchOffsetX, float touchOffsetY);
bool bounceScrollChildren( float touchOffsetX, float touchOffsetY);
void startRecordSlidAction();
virtual void endRecordSlidAction();
//ScrollViewProtocol
virtual void handlePressLogic( Touch *touch) ;
virtual void handleMoveLogic( Touch *touch) ;
virtual void handleReleaseLogic( Touch *touch) ;
virtual void interceptTouchEvent( Widget :: TouchEventType event, Widget * sender, Touch *touch) override;
void recordSlidTime( float dt);
void scrollToTopEvent();
void scrollToBottomEvent();
void scrollToLeftEvent();
void scrollToRightEvent();
void scrollingEvent();
void bounceTopEvent();
void bounceBottomEvent();
void bounceLeftEvent();
void bounceRightEvent();
protected :
Layout * _innerContainer;
Direction _direction;
Vec2 _autoScrollDir;
float _topBoundary;
float _bottomBoundary;
float _leftBoundary;
float _rightBoundary;
float _bounceTopBoundary;
float _bounceBottomBoundary;
float _bounceLeftBoundary;
float _bounceRightBoundary;
bool _autoScroll;
float _autoScrollAddUpTime;
float _autoScrollOriginalSpeed;
float _autoScrollAcceleration;
bool _isAutoScrollSpeedAttenuated;
bool _needCheckAutoScrollDestination;
Vec2 _autoScrollDestination;
bool _bePressed;
float _slidTime;
Vec2 _moveChildPoint;
float _childFocusCancelOffset;
bool _leftBounceNeeded;
bool _topBounceNeeded;
bool _rightBounceNeeded;
bool _bottomBounceNeeded;
bool _bounceEnabled;
bool _bouncing;
Vec2 _bounceDir;
float _bounceOriginalSpeed;
bool _inertiaScrollEnabled;
#include
"ui/CocosGUI.h"
//******
using
namespace
cocos2d
::
ui
;
void
touchEvent(
Ref
* pSender,
Widget
::
TouchEventType
type);
void scrollViewEvent(Ref* pSender,ScrollView::EventType type);
private
:
Size size;
Size size;
ScrollView* scrollView;
#include
"ui/CocosGUI.h"
//******
using
namespace
cocos2d
::
ui
;
size
=
Director
::
getInstance
()->
getWinSize
();
// 用于显示进度条的后背景
scrollView = ScrollView :: create ();
scrollView -> setDirection ( ScrollView :: Direction :: HORIZONTAL );
scrollView -> setBounceEnabled ( true );
scrollView -> setTouchEnabled ( true );
// 设置滚动视图大小,即可见大小
scrollView -> setSize ( Size ( 480 , 320 )); // 过时
// 设置滚动视图容器内容大小
scrollView -> setInnerContainerSize ( Size ( 480 * 3 , 320 ));
scrollView -> setPosition ( Vec2 :: ZERO );
addChild ( scrollView );
// 添加事件监听
scrollView -> addEventListener ( CC_CALLBACK_2 ( HelloWorld :: scrollViewEvent , this ));
// 往滚动视图容器中添加控件
auto iv1 = ImageView :: create ();
iv1-> loadTexture ( "HelloWorld.png" );
iv1-> setPosition ( Vec2 ( size . width / 2 , size . height / 2 ));
scrollView -> addChild (iv1);
auto iv2 = ImageView :: create ();
iv2-> loadTexture ( "HelloWorld.png" );
iv2-> setPosition ( Vec2 ( size . width * 1.5 , size . height / 2 ));
scrollView -> addChild (iv2);
auto iv3 = ImageView :: create ();
iv3-> loadTexture ( "HelloWorld.png" );
iv3-> setPosition ( Vec2 ( size . width * 2.5 , size . height / 2 ));
scrollView -> addChild (iv3);
auto btn1 = Button :: create ();
btn1-> loadTextures ( "animationbuttonnormal.png" , "animationbuttonpressed.png" , "" );
btn1-> setPosition ( Vec2 ( 480 , 160 ));
btn1-> addTouchEventListener ( CC_CALLBACK_2 ( HelloWorld :: touchEvent , this ));
btn1-> setTouchEnabled ( true );
// 用于显示进度条的后背景
scrollView = ScrollView :: create ();
scrollView -> setDirection ( ScrollView :: Direction :: HORIZONTAL );
scrollView -> setBounceEnabled ( true );
scrollView -> setTouchEnabled ( true );
// 设置滚动视图大小,即可见大小
scrollView -> setSize ( Size ( 480 , 320 )); // 过时
// 设置滚动视图容器内容大小
scrollView -> setInnerContainerSize ( Size ( 480 * 3 , 320 ));
scrollView -> setPosition ( Vec2 :: ZERO );
addChild ( scrollView );
// 添加事件监听
scrollView -> addEventListener ( CC_CALLBACK_2 ( HelloWorld :: scrollViewEvent , this ));
// 往滚动视图容器中添加控件
auto iv1 = ImageView :: create ();
iv1-> loadTexture ( "HelloWorld.png" );
iv1-> setPosition ( Vec2 ( size . width / 2 , size . height / 2 ));
scrollView -> addChild (iv1);
auto iv2 = ImageView :: create ();
iv2-> loadTexture ( "HelloWorld.png" );
iv2-> setPosition ( Vec2 ( size . width * 1.5 , size . height / 2 ));
scrollView -> addChild (iv2);
auto iv3 = ImageView :: create ();
iv3-> loadTexture ( "HelloWorld.png" );
iv3-> setPosition ( Vec2 ( size . width * 2.5 , size . height / 2 ));
scrollView -> addChild (iv3);
auto btn1 = Button :: create ();
btn1-> loadTextures ( "animationbuttonnormal.png" , "animationbuttonpressed.png" , "" );
btn1-> setPosition ( Vec2 ( 480 , 160 ));
btn1-> addTouchEventListener ( CC_CALLBACK_2 ( HelloWorld :: touchEvent , this ));
btn1-> setTouchEnabled ( true );
scrollView->addChild(btn1);
void
HelloWorld
::scrollViewEvent(
Ref
* pSender,
ScrollView
::
EventType
type){
switch (type) {
// 上下左右
case ScrollView :: EventType :: SCROLL_TO_TOP :
CCLOG ( "SCROLL_TO_TOP" );
break ;
case ScrollView :: EventType :: SCROLL_TO_BOTTOM :
CCLOG ( "SCROLL_TO_BOTTOM" );
break ;
case ScrollView :: EventType :: SCROLL_TO_LEFT :
CCLOG ( "SCROLL_TO_LEFT" );
break ;
case ScrollView :: EventType :: SCROLL_TO_RIGHT :
CCLOG ( "SCROLL_TO_RIGHT" );
break ;
//********
case ScrollView :: EventType :: SCROLLING :
CCLOG ( "SCROLLING" );
break ;
case ScrollView :: EventType :: BOUNCE_TOP :
CCLOG ( "BOUNCE_TOP" );
break ;
case ScrollView :: EventType :: BOUNCE_BOTTOM :
CCLOG ( "BOUNCE_BOTTOM" );
break ;
case ScrollView :: EventType :: BOUNCE_LEFT :
CCLOG ( "SCROLL_TO_LEFT" );
break ;
case ScrollView :: EventType :: BOUNCE_RIGHT :
CCLOG ( "BOUNCE_RIGHT" );
break ;
}
}
void HelloWorld ::touchEvent( Ref * pSender, Widget :: TouchEventType type){
switch (type) {
// 用户第一次接触到按钮的时候触发
switch (type) {
// 上下左右
case ScrollView :: EventType :: SCROLL_TO_TOP :
CCLOG ( "SCROLL_TO_TOP" );
break ;
case ScrollView :: EventType :: SCROLL_TO_BOTTOM :
CCLOG ( "SCROLL_TO_BOTTOM" );
break ;
case ScrollView :: EventType :: SCROLL_TO_LEFT :
CCLOG ( "SCROLL_TO_LEFT" );
break ;
case ScrollView :: EventType :: SCROLL_TO_RIGHT :
CCLOG ( "SCROLL_TO_RIGHT" );
break ;
//********
case ScrollView :: EventType :: SCROLLING :
CCLOG ( "SCROLLING" );
break ;
case ScrollView :: EventType :: BOUNCE_TOP :
CCLOG ( "BOUNCE_TOP" );
break ;
case ScrollView :: EventType :: BOUNCE_BOTTOM :
CCLOG ( "BOUNCE_BOTTOM" );
break ;
case ScrollView :: EventType :: BOUNCE_LEFT :
CCLOG ( "SCROLL_TO_LEFT" );
break ;
case ScrollView :: EventType :: BOUNCE_RIGHT :
CCLOG ( "BOUNCE_RIGHT" );
break ;
}
}
void HelloWorld ::touchEvent( Ref * pSender, Widget :: TouchEventType type){
switch (type) {
// 用户第一次接触到按钮的时候触发
case Widget::TouchEventType::BEGAN:
break;
// 用户在按钮区域上移动时触发
case Widget :: TouchEventType :: MOVED :
break ;
// 用户离开按钮区域时触发
case Widget :: TouchEventType :: ENDED :
//scrollView->scrollToTopRight(0.8f, true);//Scroll diretion is not both!
//scrollView->jumpToTopRight();//Scroll diretion is not both!
scrollView -> scrollToPercentHorizontal ( 70 , 1.2f , true );
break ;
// 触摸取消时触发
case Widget :: TouchEventType :: CANCELED :
break ;
}
}