库为coco2dx 2.1.1 bate3版本 用法步骤:
1.类继承CCTableViewDelegate,类声明里实现下面方法
//CCTableViewDelegate继承自CCScrollViewDelegate
virtualvoid scrollViewDidScroll(CCScrollView* view);
virtualvoid scrollViewDidZoom(CCScrollView* view);
// 用于CCSrollView的触屏回调
virtualbool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
virtualvoid ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);
virtualvoid ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);
CCScrollView* scrollView ;
CCPoint startPos;
int scroIndex;
CCSprite* pSprite;
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0, false);
this->setTouchEnabled(true);
3.创建及初始化CCScrollView对象
CCSize size = CCDirector::sharedDirector()->getWinSize(); //屏大小为480, 320
pSprite = CCSprite::create("HelloWorld.png");//创建一张背景图,因CCScrollView对象是加载到此上面的
pSprite->setPosition(ccp(size.width/2, size.height/2) );
this->addChild(pSprite,0);
CCLayer* scrollViewLayer = CCLayer::create(); //创建个容器层
CCSprite* sp1 = CCSprite::create("HelloWorld.png");//再创建3个精灵,都加载到容器层中去,注意坐标
sp1->setPosition(ccp(size.width/2 + size.width*0, size.height/2));
scrollViewLayer->addChild(sp1);
CCSprite* sp2 = CCSprite::create("HelloWorld.png");
sp2->setPosition(ccp(size.width/2 + size.width*1, size.height/2));
scrollViewLayer->addChild(sp2);
CCSprite* sp3 = CCSprite::create("HelloWorld.png");
sp3->setPosition(ccp(size.width/2 + size.width*2, size.height/2));
scrollViewLayer->addChild(sp3);
scrollViewLayer->setContentSize(CCSize(size.width*3,size.height));//设置容器层大小
scrollView = CCScrollView::create(size, scrollViewLayer);//创建滚动视图
scrollView->setDirection(kCCScrollViewDirectionHorizontal); //方向
scrollView->setDelegate(this);
this->addChild(scrollView);
scroIndex = 0; //页数索引设为0 一共3页
//此两函数不做任何
voidHelloWorld::scrollViewDidScroll(CCScrollView* view)
{
// CCLog("scrollViewDidScroll");
}
voidHelloWorld::scrollViewDidZoom(CCScrollView* view)
{
// CCLog("scrollViewDidZoom");
}
boolHelloWorld::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent)
{
// startPos = pTouch->getLocation(); //如果CCScrollView对象是添加到this中的,则得此坐标
startPos =pSprite->convertTouchToNodeSpace(pTouch); // 转换为pSprite节点的坐标
if (!scrollView->boundingBox().containsPoint(startPos)) { //判断触点是在否在scrollView中
return false;
}
return true;
}
voidHelloWorld::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent){
}
voidHelloWorld::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent)
{
scrollView->unscheduleAllSelectors();// 停止再调整,否则划动惯性会有偏差
// CCPoint endPos = pTouch->getLocation();
CCPoint endPos = pSprite->convertTouchToNodeSpace(pTouch);//最后的坐标
CCPoint offsetPos = scrollView->getContentOffset(); // 偏移的坐标,在这儿范围是0 ~ -960
CCSize pageSize = CCSize(480, 320); // 一页的大小
CCSize scrollSize = scrollView->getContentSize(); // 容器的大小
CCPoint distancePos = ccpSub(endPos, startPos); // 划痕距离
int absPageX = (int)offsetPos.x % (int)pageSize.width; //将偏移量精确到每一页,每一页的具体偏移坐标
CCPoint adjustPos; //要调整的坐标
// <-----向左划
if( (distancePos.x <0) && (scroIndex<2)) {// 页数不是在第三页,才可翻页判断
if (abs(distancePos.x) > pageSize.width/4) {// 划痕够,可翻页
scroIndex ++; // 加一页
adjustPos =ccpSub(offsetPos, ccp(pageSize.width+absPageX,0)); //scrollView坐标越往右越负,所以翻页=偏移的X坐标+(页X坐标-偏移的X坐标)
}
else {
adjustPos =ccpSub(offsetPos, ccp(distancePos.x, 0)); //划痕不够 原来的坐标
}
scrollView->setContentOffsetInDuration(adjustPos,0.2f); //0.2f为动画执行时间
}
// 向右划 ----->
else if ((distancePos.x >0) && (scroIndex>0)) {// 页数不是在第0页,才可翻页判断
if (abs(distancePos.x) > pageSize.width/4) {// 划痕够,可翻页
scroIndex --; // 回一页
adjustPos =ccpSub(offsetPos, ccp(absPageX,0));
}
else {
adjustPos =ccpSub(offsetPos, ccp(distancePos.x, 0));// 划痕不够 原来的坐标
}
scrollView->setContentOffsetInDuration(adjustPos, 0.2f);
}
//划到最左或最右时候
else {
if ((distancePos.x >=0)) {
adjustPos =ccp(0, offsetPos.y); // 最左
}
else {
adjustPos =ccpSub(offsetPos, ccp(absPageX,0)); //最右
}
scrollView->setContentOffsetInDuration(adjustPos,0.2f);
}
}