cocos2d-x中使用ScrollView实现滑屏效果

本例参考了:http://codingnow.cn/cocos2d-x/1024.htmlhttp://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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值