最近写CCScrollView遇到很多问题,样式是竖直的类似tableview,在此记录下:
CCLayer* layer;
初始化scrollview内容器层
layer = CCLayer::create();
设置layer
layer->setPosition(ccp(, -));
初始化CCScrollView
scrollView=CCScrollView::create(CCSizeMake(, --));
scrollView->setPosition(ccp(,));
scrollView->setDelegate(this);
this->addChild(scrollView);
scrollView->setContainer(layer);
scrollView->setContentSize(CCSizeMake(, -));
scrollView->setDirection(kCCScrollViewDirectionVertical);
scrollView->setTouchEnabled(false);
校对和调整回滚
bool HandbookManager::ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent )
{
CCPoint startPos = pTouch->getLocation(); return true;
} void HandbookManager::ccTouchMoved( CCTouch *pTouch, CCEvent *pEvent )
{
CCPoint currPos = pTouch->getLocation();
CCPoint prevPos = pTouch->getPreviousLocation(); CCLayer * info_listbg = (CCLayer*)scrollView->getContainer(); if(scrollView->boundingBox().containsPoint(currPos))
{
float y = info_listbg->getPositionY();
info_listbg->setPositionY(y+currPos.y-prevPos.y);
}
} void HandbookManager::ccTouchEnded( CCTouch *pTouch, CCEvent *pEvent )
{
CCPoint touchPoint = convertTouchToNodeSpace(pTouch);
touchPoint = ccpAdd(this->getPosition(), touchPoint);
float dx=fabs(pTouch->getStartLocation().x-pTouch->getLocation().x);
float dy=fabs(pTouch->getStartLocation().y-pTouch->getLocation().y); if(dx< && dy<){
this->touchPoint(touchPoint);
} CCSprite * s1 = (CCSprite*)layer->getChildByTag();//取出层上最上精灵元素
CCSprite * s2 = (CCSprite*)layer->getChildByTag(flagTag);//取出层上最下精灵元素元素 p1.y -= - layer->getPositionY();
p2.y -= - layer->getPositionY()+; //firstSpritePosition为层上最上面元素的position
//lastSpritePosition 为层上最下面元素的position float y1 = firstSpritePosition.y;
float y2;
if(lastSpritePosition.y>firstSpritePosition.y-scrollView->boundingBox().size.height)
y2 = lastSpritePosition.y;
else
y2 = firstSpritePosition.y-scrollView->boundingBox().size.height; if(p1.y <= y1){
CCActionInterval * action = CCMoveBy::create((y1-p1.y)/,ccp(,y1-p1.y));
layer->runAction(action);
}
if(p2.y >= y2){ CCActionInterval * action = CCMoveBy::create((p2.y-y2)/,ccp(,y2-p2.y));
layer->runAction(action); }
}
//取得scrollview中点击区域
CCRect HandbookManager::getRect(CCNode * pNode)
{
CCRect rc;
rc.origin = pNode->getPosition();
rc.size = pNode->boundingBox().size;
rc.origin.x -= rc.size.width*0.5 - layer->getPositionX();
rc.origin.y -= rc.size.height*0.5- layer->getPositionY(); rc.origin.x += ;
rc.origin.y += ;
return rc;
}