cocos2dx 2.0 CCScrollView的用法以及滑动的原理

时间:2023-03-10 06:03:20
cocos2dx 2.0 CCScrollView的用法以及滑动的原理
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC;
#include "cocos-ext.h"
USING_NS_CC_EXT;
// 校正滑动动画速度
#define ADJUST_ANIM_VELOCITY 800; class HelloWorld : public cocos2d::CCLayer, public cocos2d::extension::CCScrollViewDelegate
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene(); // a selector callback
void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually
CREATE_FUNC(HelloWorld); public:
void scrollViewDidScroll(cocos2d::extension::CCScrollView* view);
void scrollViewDidZoom(CCScrollView* view); public:
virtual void onEnter();
virtual void onExit(); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); private:
// 当触摸事件结束时,校正CCScrollView的位置
void adjustScrollView(); private:
// 菜单回调
void menu1Callback(CCNode *pSender);
void menu2Callback(CCNode *pSender); private:
// 显示当前页
CCLabelTTF* pLabel; CCScrollView *scrollView; }; #endif // __HELLOWORLD_SCENE_H__

实现文件

 #include "HelloWorldScene.h"
#include "SimpleAudioEngine.h" using namespace cocos2d;
using namespace CocosDenshion;
USING_NS_CC_EXT; CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
}
CCLayer *layer;
CCSprite *sprite2;
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. // add a "close" icon to exit the progress. it's an autorelease object // ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize(); layer = CCLayerColor::create(ccc4(, , ,));
layer->setAnchorPoint(CCPointZero);
layer->setPosition(CCPointZero); // CCScrollView
scrollView = CCScrollView::create(CCSizeMake(, ), layer); // scrollView->setBounceable(false); CCSprite *sprite1 = CCSprite::create("HelloWorld.png");
sprite2 = CCSprite::create("HelloWorld.png");
CCSprite *sprite3 = CCSprite::create("HelloWorld.png"); // Menu
//CCMenuItemSprite *menuItem1 = CCMenuItemSprite::create(sprite1, sprite1, this, menu_selector(HelloWorld::menu1Callback));
// CCMenuItemSprite *menuItem1 = CCMenuItemSprite::create(sprite1, sprite1, this, NULL);
// menuItem1->setPosition(ccpAdd(CCPointZero, ccp(size.width / 2, size.height / 2)));
//
// CCMenuItemSprite *menuItem2 = CCMenuItemSprite::create(sprite2, sprite2, this, NULL);
// menuItem2->setPosition(ccpAdd(ccp(480, 0), ccp(size.width / 2, size.height / 2)));
//
// CCMenu *menu = CCMenu::create(menuItem1, menuItem2, NULL);
//
// menu->setPosition(CCPointZero);
// layer->addChild(menu); sprite1->setPosition(ccpAdd(CCPointZero, ccp(size.width / , size.height / )));
sprite2->setPosition(ccpAdd(ccp(, ), ccp(size.width / , size.height / )));
sprite3->setPosition(ccpAdd(ccp(, ), ccp(size.width / , size.height / ))); layer->addChild(sprite1);
layer->addChild(sprite2);
layer->addChild(sprite3); layer->setContentSize(CCSizeMake(, )); sprite1->setScale(0.5);
sprite2->setScale(0.5); scrollView->setPosition(CCPointZero);
scrollView->setContentOffset(CCPointZero); scrollView->setContentSize(CCSizeMake(*, ));
scrollView->setContainer(layer); scrollView->setDirection(kCCScrollViewDirectionHorizontal);
scrollView->setDelegate(this); this->addChild(scrollView); return true;
} void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit();
#endif
} void HelloWorld::scrollViewDidScroll(cocos2d::extension::CCScrollView *view)
{
} void HelloWorld::scrollViewDidZoom(cocos2d::extension::CCScrollView *view)
{
} void HelloWorld::onEnter()
{
CCLayer::onEnter();
// 这里的第三个参数一定要设置成false,
// true 即HelloWorld层吞噬掉触摸事件
// false 即HelloWorld层与CCScrollView对象先后处理触摸事件
// CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, false);
} void HelloWorld::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
} bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
auto postion= pTouch->getLocation();
auto p2= layer->convertToNodeSpace(postion);
// printf("p1.x=%f,p1.y=%f\n",postion.x,postion.y);
// printf("p2.x=%f,p2.y=%f\n",p2.x,p2.y); auto rect =sprite2->boundingBox();
if(rect.containsPoint(p2)){
int ffff=;
ffff++;
// printf("摸到了\n");
} int ffffr=;
ffffr++; return true;
} void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
// CCLOG("move");
} void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
adjustScrollView();
} void HelloWorld::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
adjustScrollView();
} void HelloWorld::adjustScrollView()
{
// 关闭CCScrollView中的自调整
scrollView->unscheduleAllSelectors(); int x = scrollView->getContentOffset().x;
int offset = (int) x % ; printf("x=%d,offset=%d\n",x,offset);
// 调整位置
CCPoint adjustPos;
// 调整动画时间
float adjustAnimDelay; if (offset < -) {
// 计算下一页位置,时间
adjustPos = ccpSub(scrollView->getContentOffset(), ccp( + offset, ));
adjustAnimDelay = (float) ( + offset) / ADJUST_ANIM_VELOCITY;
printf("-240调整后的坐标=%f\n",adjustPos.x);
}
else {
// 计算当前页位置,时间
adjustPos = ccpSub(scrollView->getContentOffset(), ccp(offset, )); // 这里要取绝对值,否则在第一页往左翻动的时,保证adjustAnimDelay为正数
adjustAnimDelay = (float) abs(offset) / ADJUST_ANIM_VELOCITY; printf("现在的位移=%f\n",scrollView->getContentOffset().x); printf(">240调整后的坐标=%f\n",adjustPos.x);
} // 调整位置
scrollView->setContentOffsetInDuration(adjustPos, adjustAnimDelay);
} void HelloWorld::menu1Callback(cocos2d::CCNode *pSender)
{
CCLOG("menu1Callback");
} void HelloWorld::menu2Callback(cocos2d::CCNode *pSender)
{
CCLOG("menu2Callback");
}

这里主要研究的是他的显示,以及当滑动停止的时候,有个加速度还要滑动一段,具体如何实现,是研究的CCScrollView源码,如下

 void CCScrollView::deaccelerateScrolling(float dt)
{
if (m_bDragging)
{
this->unschedule(schedule_selector(CCScrollView::deaccelerateScrolling));
return;
} float newX, newY;
CCPoint maxInset, minInset; m_pContainer->setPosition(ccpAdd(m_pContainer->getPosition(), m_tScrollDistance)); if (m_bBounceable)
{
maxInset = m_fMaxInset;
minInset = m_fMinInset;
}
else
{
maxInset = this->maxContainerOffset();
minInset = this->minContainerOffset();
}
printf("maxinset.x=%f,mininset.x=%f\n",maxInset.x,minInset.x);
//96,-1056 //check to see if offset lies within the inset bounds
newX = MIN(m_pContainer->getPosition().x, maxInset.x);
newX = MAX(newX, minInset.x);
newY = MIN(m_pContainer->getPosition().y, maxInset.y);
newY = MAX(newY, minInset.y); newX = m_pContainer->getPosition().x;
newY = m_pContainer->getPosition().y; m_tScrollDistance = ccpSub(m_tScrollDistance, ccp(newX - m_pContainer->getPosition().x, newY - m_pContainer->getPosition().y));
m_tScrollDistance = ccpMult(m_tScrollDistance, SCROLL_DEACCEL_RATE);
this->setContentOffset(ccp(newX,newY)); if ((fabsf(m_tScrollDistance.x) <= SCROLL_DEACCEL_DIST &&
fabsf(m_tScrollDistance.y) <= SCROLL_DEACCEL_DIST) ||
newY > maxInset.y || newY < minInset.y ||
newX > maxInset.x || newX < minInset.x ||
newX == maxInset.x || newX == minInset.x ||
newY == maxInset.y || newY == minInset.y)
{
this->unschedule(schedule_selector(CCScrollView::deaccelerateScrolling));
this->relocateContainer(true);
}
}

每次坐标增加

m_tScrollDistance,但是
m_tScrollDistance会每次变为0.95*m_tScrollDistance,达到减速的效果