本例參考了:http://codingnow.cn/cocos2d-x/1024.html和http://blog.csdn.net/toss156/article/details/7884207
主要代碼:
(1)GalleryLayer.h
#pragma
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
class GalleryLayer : public CCLayer,public CCScrollViewDelegate{
public:
CREATE_FUNC(GalleryLayer);
virtual bool init();
void menuCloseCallback(CCObject* pSender);
public:
//scrollview滾動的時候會調用
void scrollViewDidScroll(CCScrollView* view);
//scrollview縮放的時候會調用
void scrollViewDidZoom(CCScrollView* view);
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:
//根據手勢滑動的距離和方向滾動圖層
void adjustScrollView(float offset);
CCScrollView* m_scrollView;
CCPoint m_touchPoint;
int m_curPage;
};
(2)
GalleryLayer.cpp
#include "GalleryLayer.h"
bool GalleryLayer::init(){
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
//初始化當前頁
m_curPage = 1;
CCSize visibaleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
//添加背景
CCSprite* bg = CCSprite::create("Help_BG-hd.png");
bg->setAnchorPoint(CCPointZero);
bg->setPosition(CCPointZero);
bg->setScaleY(0.5f);
this->addChild(bg);
CCLayer* p_Layer = CCLayer::create();
//爲p_Layer創建內容
for (int i = 1; i <= 6; i++)
{
CCString* str = CCString::createWithFormat("Help_0%d_chs-hd.png",i);
CCSprite* sprite = CCSprite::create(str->getCString());
sprite->setScaleX(1.0);
sprite->setScaleY(0.5);
sprite->setPosition(ccp(visibaleSize.width * (i - 0.5f), visibaleSize.height/2 + 25));
p_Layer->addChild(sprite);
}
//設置p_Layer的內容大小、位置等
p_Layer->setContentSize(CCSizeMake(480*6, 320));
//創建ScrollView
m_scrollView = CCScrollView::create(CCSizeMake(480, 320),p_Layer);
m_scrollView->setContentOffset(CCPointZero);//充當了錨點的作用
m_scrollView->setTouchEnabled(false);//此處必須設爲false,如果設爲true,會影響滑動的效果
m_scrollView->setDelegate(this);
m_scrollView->setDirection(kCCScrollViewDirectionHorizontal);
this->addChild(m_scrollView);
//創建當前頁標識
CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFrame(CCSpriteFrame::create("Help_Point01-hd.png",CCRectMake(0,0,32,32)),"Help_Point01-hd.png");
cache->addSpriteFrame(CCSpriteFrame::create("Help_Point02-hd.png",CCRectMake(0,0,32,32)),"Help_Point02-hd.png");
for (int i=1; i<=6; i++)
{
CCSprite* point = CCSprite::createWithSpriteFrameName("Help_Point01-hd.png");
point->setScale(0.5f);
point->setTag(100+i);
point->setPosition(ccp(160+25*i,30));
this->addChild(point);
}
CCSprite* point = (CCSprite *)this->getChildByTag(101);
point->setDisplayFrame(cache->spriteFrameByName("Help_Point02-hd.png"));
point->setScale(0.5f);
bRet = true;
} while (0);
return bRet;
}
void GalleryLayer::menuCloseCallback(CCObject* pSender){
}
void GalleryLayer::scrollViewDidScroll(CCScrollView* view){
CCLOG("scroll");
}
void GalleryLayer::scrollViewDidZoom(CCScrollView* view){
CCLOG("zoom");
}
void GalleryLayer::onEnter(){
CCLayer::onEnter();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,1,false);
}
void GalleryLayer::onExit(){
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames();
}
bool GalleryLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
m_touchPoint = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
return true;
}
void GalleryLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){
}
void GalleryLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){
CCPoint endPoint = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
float distance = endPoint.x - m_touchPoint.x;
if (fabs(distance) > 50)
{
adjustScrollView(distance);
}
}
void GalleryLayer::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent){
}
void GalleryLayer::adjustScrollView(float offset){
//邏輯窗口大小中能顯示在屏幕上的邏輯大小
CCSize visibaleSize = CCDirector::sharedDirector()->getVisibleSize();
//邏輯中與於屏幕左下角對應的點
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
CCSprite *point = (CCSprite *)this->getChildByTag(100+m_curPage);
point->setDisplayFrame(cache->spriteFrameByName("Help_Point01-hd.png"));
//判斷將要顯示的是上一頁還是下一頁
if (offset < 0)
{
m_curPage++;
}else{
m_curPage--;
}
if (m_curPage < 1)
{
m_curPage = 1;
}
if (m_curPage > 6)
{
m_curPage = 6;
}
point = (CCSprite*)this->getChildByTag(100+m_curPage);
point->setDisplayFrame(cache->spriteFrameByName("Help_Point02-hd.png"));
CCPoint adjustPos = ccp(origin.x - (m_curPage - 1) * visibaleSize.width,0);
m_scrollView->setContentOffset(adjustPos, true);
}
(3)代碼實例下載地址
http://download.csdn.net/detail/zebinaibude/6829999