1
2
|
#include "cocos-ext.h"
using
namespace
cocos2d::extension;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class
CCScrollView :
public
CCLayer
/**
* 2种创建方式
*/
//会自动创建CCLayer作为容器
static
CCScrollView* create();
//size: 滚动视图的可视区域大小
//container:自定义滚动视图的CCLayer容器
static
CCScrollView* create(CCSize size, CCNode* container = NULL);
//举例
CCLayer* scrollLayer = CCLayer::create();
CCScrollView* scrollView = CCScrollView::create(CCSizeMake(150, 100), scrollLayer);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
/**
* 容器相关操作
* setContainer , setContentSize , setContentOffset
*/
//设置滚动视图的容器
void
setContainer(CCNode* pContainer);
CCNode* getContainer();
//设置容器Container的尺寸大小
virtual
void
setContentSize(
const
CCSize & size);
virtual
const
CCSize& getContentSize()
const
;
//设置容器相对滚动视图的偏移量
//animated为是否附带滑动的动作效果,还是直接设置为新的偏移量
//默认滑动动作为0.15秒,从旧位置滑动到新位置.
void
setContentOffset(CCPoint offset,
bool
animated =
false
);
void
setContentOffsetInDuration(CCPoint offset,
float
dt);
CCPoint getContentOffset();
/**
* 滚动视图相关操作
* setViewSize , setDirection , setZoomScale ,
* isDragging , isTouchMoved , setBounceable , setTouchEnabled
*/
//设置滚动视图可视区域的大小
void
setViewSize(CCSize size);
CCSize getViewSize();
//设置滚动视图允许滚动的方向
//CCScrollViewDirection:
// kCCScrollViewDirectionBoth 横向纵向都能滚动,默认方式
// kCCScrollViewDirectionHorizontal 只能横向滚动
// kCCScrollViewDirectionVertical 只能纵向滚动
virtual
void
setDirection(CCScrollViewDirection eDirection);
CCScrollViewDirection getDirection();
//放缩滚动视图大小
//好像有bug,建议不要使用了!
void
setZoomScale(
float
s);
void
setZoomScale(
float
s,
bool
animated);
float
getZoomScale();
void
setZoomScaleInDuration(
float
s,
float
dt);
bool
isDragging();
//用户是否正在CCScrollView中操作
bool
isTouchMoved();
//用户是否在CCScrollView中移动
void
setBounceable(
bool
bBounceable);
//是否开启弹性效果
bool
isBounceable();
//是否具有弹性效果
void
setTouchEnabled(
bool
e);
//是否开启触摸
|
1
2
|
virual
void
scrollViewDidScroll(CCScrollView * view);
//有滚动时的响应函数
virual
void
scrollViewDidZoom(CCScrollView * view);
//有缩放时的响应函数
|
1
2
3
4
5
6
7
8
|
//设置滚动视图的事件委托代理对象,一般为this
//并且CCLayer必需要继承代理接口类CCScrollViewDelegate
void
setDelegate(CCScrollViewDelegate* pDelegate);
CCScrollViewDelegate* getDelegate();
//举例:
//scrollView->setDelegate(this);
|
1
2
3
|
void
test1();
//测试图片滚动
void
test2();
//测试只能纵向滚动
void
test3();
//测试背包翻页
|
1
2
|
#include "cocos-ext.h"
using
namespace
cocos2d::extension;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class
HelloWorld :
public
cocos2d::CCLayer,
public
CCScrollViewDelegate
{
public
:
virtual
bool
init();
static
cocos2d::CCScene* scene();
void
menuCloseCallback(CCObject* pSender);
CREATE_FUNC(HelloWorld);
void
test1();
//测试图片滚动
void
test2();
//测试只能纵向滚动
void
test3();
//测试背包翻页
int
pageNumber;
//背包第几页
CCMenuItemImage* pBack;
//往前翻页
CCMenuItemImage* pFront;
//往后翻页
void
scrollImage(CCObject* sender);
//test3的背包翻页
void
scrollViewDidScroll(CCScrollView* view);
//滚动时响应的回调函数
void
scrollViewDidZoom(CCScrollView* view);
//放缩时响应的回调函数
};
|
1
2
3
4
5
6
7
8
|
void
HelloWorld::scrollViewDidScroll(CCScrollView* view)
{
CCLOG(
"ScrollView Moved!"
);
}
void
HelloWorld::scrollViewDidZoom(CCScrollView* view)
{
CCLOG(
"ScrollView Scaled"
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
void
HelloWorld::test1()
{
CCSize visableSize = CCSizeMake(480, 320);
//屏幕大小
CCSize mysize = CCSizeMake(960,600);
//容器大小
//创建容器、设置大小
CCLayerColor* scrollLayer = CCLayerColor::create( ccc4(255,255,255,255) );
scrollLayer->setContentSize(mysize);
//容器中的东西
CCSprite* bg = CCSprite::create(
"war.png"
);
bg->setPosition( ccp(960/2.0, 600/2.0) );
scrollLayer->addChild(bg);
//创建滚动视图CCScrollView
CCScrollView* scrollView = CCScrollView::create();
this
->addChild(scrollView, 0, 1);
//属性设置
scrollView->setContainer(scrollLayer);
//设置容器
scrollView->setViewSize( visableSize );
//可视区域大小
scrollView->setBounceable(
true
);
//是否具有弹性
//委托代理
scrollView->setDelegate(
this
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
void
HelloWorld::test2()
{
CCSize visableSize = CCSizeMake(480, 320);
//屏幕大小
CCSize mysize = CCSizeMake(150,220);
//容器大小
//创建容器、设置大小
CCLayerColor* scrollLayer = CCLayerColor::create( ccc4(255,255,255,255) );
scrollLayer->setContentSize(mysize);
//容器中添加四个按钮
for
(
int
i = 1; i <= 4; i++)
{
char
file[20];
sprintf
(file,
"btn%d.png"
, i);
CCSprite* btn = CCSprite::create(file);
btn->setPosition( ccp(mysize.width/2, 220 - 50*i) );
scrollLayer->addChild(btn);
}
//创建滚动视图CCScrollView
//可视区域大小150*100 、 容器为scrollLayer
CCScrollView* scrollView = CCScrollView::create(CCSizeMake(150, 100), scrollLayer);
scrollView->setPosition( visableSize/2 - ccp(150/2.0, 100/2.0) );
this
->addChild(scrollView, 0, 2);
//设置为只能纵向滚动
scrollView->setDirection(kCCScrollViewDirectionVertical);
//委托代理
scrollView->setDelegate(
this
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
void
HelloWorld::test3()
{
CCSize visableSize = CCSizeMake(480, 320);
//屏幕大小
CCSize mysize = CCSizeMake(300,80);
//容器大小
//创建容器、设置大小
CCLayerColor* scrollLayer = CCLayerColor::create( ccc4(255,255,255,255) );
scrollLayer->setContentSize(mysize);
//容器中添加三个图片
for
(
int
i = 1; i <= 3; i++)
{
char
file[20];
sprintf
(file,
"sp%d.png"
, i);
CCSprite* sp = CCSprite::create(file);
sp->setPosition( ccp(100*i - 50, 40) );
scrollLayer->addChild(sp);
}
//创建滚动视图CCScrollView
//可视区域大小100*80 、 容器为scrollLayer
CCScrollView* scrollView = CCScrollView::create(CCSizeMake(100, 80), scrollLayer);
scrollView->setPosition( visableSize/2 - ccp(100/2.0, 0) );
this
->addChild(scrollView, 0, 3);
//属性设置
scrollView->setTouchEnabled(
false
);
//关闭触碰事件,无法触摸滚动
//委托代理
scrollView->setDelegate(
this
);
//创建背包翻页按钮
//前翻pBack、后翻pFront
pBack = CCMenuItemImage::create(
"b1.png"
,
"b2.png"
,
"b3.png"
,
this
, menu_selector(HelloWorld::scrollImage) );
pFront = CCMenuItemImage::create(
"f1.png"
,
"f2.png"
,
"f3.png"
,
this
, menu_selector(HelloWorld::scrollImage) );
pBack->setPosition( ccp(visableSize.width/2 - 100, 60) );
pFront->setPosition( ccp(visableSize.width/2 + 100, 60) );
CCMenu* pMenu = CCMenu::create(pBack, pFront, NULL);
pMenu->setPosition(CCPointZero);
this
->addChild(pMenu, 0, 100);
pBack->setEnabled(
false
);
pageNumber = 0;
//第0页
}
//实现翻页效果scrollImage
void
HelloWorld::scrollImage(CCObject* sender)
{
CCScrollView* scrollView = (CCScrollView*)
this
->getChildByTag(3);
if
(sender == pBack && pBack->isEnabled() )
{
pageNumber = max(0, pageNumber-1);
//前翻
}
else
if
( pFront->isEnabled() )
{
pageNumber = min(2, pageNumber+1);
//后翻
}
//设置容器相对滚动视图的偏移量
scrollView->setContentOffset(ccp(-100 * pageNumber, 0),
true
);
pBack->setEnabled( pageNumber != 0);
pFront->setEnabled( pageNumber != 2);
}
|