http://m18701641797.blog.163.com/blog/static/21618400820143492614699/
截图就先不上啦,基本说明一下吧:该控件是cocos2d-x的扩展控件,主要使用在显示内容上,并且是那种需要拖动显示的地方,比如,使用说明这个地方,最容易使用到。好了,上代码吧。
ScrollScene.cpp#ifndef __SCROLL_SCENE__ #define __SCROLL_SCENE__ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; class ScrollScene : public CCLayer, public extension::CCScrollViewDelegate{ public: ScrollScene(); virtual bool init(); static CCScene* scene(); virtual void scrollViewDidScroll(extension::CCScrollView* view); virtual void scrollViewDidZoom(extension::CCScrollView* view); virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event); virtual void ccTouchMoved(CCTouch* touch, CCEvent* event); virtual void ccTouchEnded(CCTouch* touch, CCEvent* event); CREATE_FUNC(ScrollScene); private: CCSize winSize; CCPoint centerCcp; extension::CCScrollView* scrollView; short curScrollIndex; }; #endif
#include "ScrollScene.h" #include "cocos-ext.h" #define BG_WIDTH 480 #define BG_HEIGHT 320 ScrollScene::ScrollScene() { winSize = CCDirector::sharedDirector()->getWinSize(); centerCcp = CCPoint(winSize.width/2, winSize.height/2); curScrollIndex = 0; } bool ScrollScene::init() { CCLayer::init(); scrollView = extension::CCScrollView::create();//创建一个scrollView CCLayer* containerLayer = CCLayer::create(); CCSprite *sprite1 = CCSprite::create("imageA.png"); sprite1->setPosition(ccp(240 + 0 * 480, 160)); containerLayer->addChild(sprite1); CCSprite *sprite2 = CCSprite::create("imageB.png"); sprite2->setPosition(ccp(240 + 1 * 480, 160)); containerLayer->addChild(sprite2); CCSprite* sprite3 = CCSprite::create("HelloWorld.png"); sprite3->setPosition(ccp(240 + 2*480, 160)); containerLayer->addChild(sprite3); containerLayer->setAnchorPoint(CCPointZero); containerLayer->setPosition(CCPointZero); scrollView->setAnchorPoint(CCPointZero); scrollView->setPosition(CCPointZero); // 显示显示的区域 scrollView->setViewSize(CCSizeMake(480, 320));//设置view的大小 scrollView->setContentOffset(CCPointZero); containerLayer->setContentSize(CCSizeMake(1440, 320));//设置滚动区域的大小 // 显示滑动的区域大小 scrollview的实际大小 scrollView->setContentSize(CCSizeMake(1440, 320));//设置scrollview区域的大小 scrollView->setContainer(containerLayer); //设置需要滚动的内容 //因为要自己实现触摸消息,所以这里设为false
scrollView->setTouchEnabled(false); scrollView->setDirection(extension::kCCScrollViewDirectionHorizontal); //设置滚动的方向,有三种可以选择 scrollView->setDelegate(this); this->addChild(scrollView); scrollView->setScaleX(winSize.width/BG_WIDTH); scrollView->setScaleY(winSize.height/BG_HEIGHT); CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, true, this); return true; } CCScene* ScrollScene::scene() { CCScene* scene = CCScene::create(); ScrollScene* layer = ScrollScene::create(); scene->addChild(layer); return scene; } // 这两个方法,只需要实现就好,不用写任何操作 void ScrollScene::scrollViewDidScroll(extension::CCScrollView* view) { } void ScrollScene::scrollViewDidZoom(extension::CCScrollView* view) { } bool ScrollScene::ccTouchBegan(CCTouch* touch, CCEvent* event) { CCPoint currentCcp = touch->getLocation(); if (!scrollView->boundingBox().containsPoint(currentCcp)) { return false; } return true; } void ScrollScene::ccTouchMoved(CCTouch* touch, CCEvent* event) { } void ScrollScene::ccTouchEnded(CCTouch* touch, CCEvent* event) { CCPoint startCcp = touch->getStartLocation(); CCPoint endCcp = touch->getLocation(); CCPoint adjustCcp = scrollView->getContentOffset(); if (endCcp.x - startCcp.x > 0 && curScrollIndex > 0) { adjustCcp = ccpSub(scrollView->getContentOffset(), ccp(-480, 0)); curScrollIndex--; } if (endCcp.x - startCcp.x < 0 && curScrollIndex < 2) { adjustCcp = ccpSub(scrollView->getContentOffset(), ccp(480, 0)); curScrollIndex++; } // 这里把时间设置为0.2f是有含义的,如果将时间设置的过长,在用户滑动过快的过程中,会连续触发该方法///,会导致视图出错,如果实在是想要把时间调整的大一点,比如1.0f,可以这是一个状态,判断是否在scrollView是否//在滑动过程中,在判断处理一下即可!(这里为了省劲,就直接写了一个相对较小的时间段); scrollView->setContentOffsetInDuration(adjustCcp, 0.2f); }