cocos2dx 屏幕滑动(ScrollViewDelegate、TableViewDelegate)和屏幕触摸响应

前端之家收集整理的这篇文章主要介绍了cocos2dx 屏幕滑动(ScrollViewDelegate、TableViewDelegate)和屏幕触摸响应前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.ScrollView

using namespace cocos2d;
using namespace cocos2d::extension;

bool GalleryLayer::init()
{
	bool bRet = false;
	do
	{
       CC_BREAK_IF( !CCLayer::init() );

	   m_nCurPage = 1;
	   CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	   CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

	   Layer *pLayer = Layer::create();
	   char helpstr[30] = {0};
	   for (int i = 1; i <= 5; ++ i)
	   {
		   memset(helpstr,sizeof(helpstr));
		   sprintf(helpstr,"bg_%02d.jpg",i);
		   Sprite *pSprite = Sprite::create(helpstr,Rect(300,1600,1080));
		   pSprite->setScaleX(visibleSize.width / pSprite->getContentSize().width);
		   pSprite->setScaleY(visibleSize.height / pSprite->getContentSize().height);
		   pSprite->setAnchorPoint(Vec2::ZERO);
		   pSprite->setPosition(Vec2(visibleSize.width * ( i -1),0));
		   pLayer->addChild(pSprite);
	   }
	   
	   m_pScrollView = ScrollView::create(CCSizeMake(360,640),pLayer);
	   m_pScrollView->setContentOffset(Vec2::ZERO);
	   m_pScrollView->setTouchEnabled(false);
	   m_pScrollView->setDelegate(this);
	   m_pScrollView->setDirection(ScrollView::Direction::HORIZONTAL);
	   pLayer->setContentSize(CCSizeMake(360*5,640));

	   this->addChild(m_pScrollView);

	  /* CCSpriteFrameCache *pCache =  CCSpriteFrameCache::sharedSpriteFrameCache();
   
	   pCache->addSpriteFrame(CCSpriteFrame::create("button_normal.png",CCRectMake(0,64,64)),"button_normal.png");
	   pCache->addSpriteFrame(CCSpriteFrame::create("button_selected.png","button_selected.png");
	   for (int i = 1; i <= 5; ++ i)
	   {
		   CCSprite *pPoint = CCSprite::createWithSpriteFrameName("button_normal.png");
		   pPoint->setTag(i);
		   pPoint->setPosition(ccp( origin.x + (visibleSize.width - 3 * pPoint->getContentSize().width)/2 + pPoint->getContentSize().width * (i-1),origin.y + 30));
		   this->addChild(pPoint);
	   }
		CCSprite *pPoint = (CCSprite *)this->getChildByTag(1);
		pPoint->setDisplayFrame(pCache->spriteFrameByName("button_selected.png"));*/
	
		bRet = true;
	}while(0);



	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = CC_CALLBACK_2(GalleryLayer::TouchBegan,this);
	listener->onTouchMoved = CC_CALLBACK_2(GalleryLayer::TouchMoved,this);
	listener->onTouchEnded = CC_CALLBACK_2(GalleryLayer::TouchEnded,this);
	listener->onTouchCancelled = CC_CALLBACK_2(GalleryLayer::TouchCancelled,this);

	_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this);

	return bRet;
}

void GalleryLayer::menuCloseCallback(CCObject* pSender)
{

}

void GalleryLayer::scrollViewDidScroll(cocos2d::extension::ScrollView *view)
{
	CCLOG("scroll");
}

void GalleryLayer::scrollViewDidZoom(cocos2d::extension::ScrollView *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::TouchBegan(CCTouch *pTouch,CCEvent *pEvent)
{
	m_touchPoint = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
	return true;
}

void GalleryLayer::TouchMoved(CCTouch *pTouch,CCEvent *pEvent)
{
	//m_pScrollView->ccTouchMoved(pTouch,pEvent);

	//m_pScrollView->setContentOffset(Vec2::ZERO);
}

void GalleryLayer::TouchEnded(CCTouch *pTouch,CCEvent *pEvent)
{
	CCPoint endPoint = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
	float distance = endPoint.x - m_touchPoint.x;
	//if(fabs(distance) > 20)
	//{
		adjustScrollView(distance);
	//}
}

void GalleryLayer::TouchCancelled(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)
{
	CCPoint endPoint = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
	float distance = endPoint.x - m_touchPoint.x;
	//if(fabs(distance) > 20)
	//{
		adjustScrollView(distance);
	//}
}

void GalleryLayer::adjustScrollView(float offset)
{
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	//CCSpriteFrameCache *pCache =  CCSpriteFrameCache::sharedSpriteFrameCache();
	//CCSprite *pPoint = (CCSprite *)this->getChildByTag(m_nCurPage);
	//if(!pPoint)
	//{
	//	return;
	//}

	/*if (fabs(offset) < 180)
	{

	}
	else */if (offset < 0)
	{
		m_nCurPage ++;
	}
	else
	{
		m_nCurPage --;
	}

	if (m_nCurPage < 1)
	{
		m_nCurPage = 1;
	}
	if(m_nCurPage > 5)
	{
		m_nCurPage = 5;
	}
	
	//pPoint = (CCSprite *)this->getChildByTag(m_nCurPage);
	//pPoint->setDisplayFrame(pCache->spriteFrameByName("button_selected.png"));
	CCPoint  adjustPos = ccp(origin.x - visibleSize.width * (m_nCurPage-1),0);	
	//m_pScrollView->setContentOffsetInDuration(adjustPos,0.3f);
	m_pScrollView->setContentOffset(adjustPos);
	
}


#ifndef __HELP_SCENE_H__
#define __HELP_SCENE_H__

#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "extensions/cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;


class GalleryLayer : public cocos2d::CCLayer,public ScrollViewDelegate
{
public:
    virtual bool init();  

    void menuCloseCallback(CCObject* pSender);

    CREATE_FUNC(GalleryLayer);

public:
	//scrollview滚动的时候会调用
	void scrollViewDidScroll(ScrollView* view);
	//scrollview缩放的时候会调用
	void scrollViewDidZoom(ScrollView* view);

	virtual void onEnter();
	virtual void onExit();

	virtual bool TouchBegan(CCTouch *pTouch,CCEvent *pEvent);
	virtual void TouchMoved(CCTouch *pTouch,CCEvent *pEvent);
	virtual void TouchEnded(CCTouch *pTouch,CCEvent *pEvent);
	virtual void TouchCancelled(CCTouch *pTouch,CCEvent *pEvent);

private:
	//根据手势滑动的距离和方向滚动图层
	 void adjustScrollView(float offset);
	 ScrollView *m_pScrollView;
	 CCPoint m_touchPoint;
	 int m_nCurPage;
};

#endif  

2.TableView

#ifndef __TABLEVIEWTESTSCENE_H__
#define __TABLEVIEWTESTSCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"

class ListViewLayer : public cocos2d::CCLayer,public cocos2d::extension::TableViewDataSource,public cocos2d::extension::TableViewDelegate
{
public:
    virtual bool init();  
    
    virtual void scrollViewDidScroll(cocos2d::extension::ScrollView* view);

    virtual void scrollViewDidZoom(cocos2d::extension::ScrollView* view);

	//处理触摸事件
    virtual void tableCellTouched(cocos2d::extension::TableView* table,cocos2d::extension::TableViewCell* cell);
	//每一项的宽度和高度
    virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::TableView *table);
	//生成列表每一项的内容
    virtual cocos2d::extension::TableViewCell* tableCellAtIndex(cocos2d::extension::TableView *table,ssize_t idx);
	//一共多少项
    virtual ssize_t numberOfCellsInTableView(cocos2d::extension::TableView *table);

    CREATE_FUNC(ListViewLayer);
};

#endif 

#include "ListViewLayer.h"

USING_NS_CC;
USING_NS_CC_EXT;


bool ListViewLayer::init()
{
	bool bRet = false;
	do
	{
		CC_BREAK_IF( !CCLayer::init() );

		TableView* pTableView = TableView::create(this,CCSizeMake(960,640));
		pTableView->setDirection(ScrollView::Direction::HORIZONTAL);
		pTableView->setPosition(CCPointZero);
		pTableView->setDelegate(this);
		pTableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
		this->addChild(pTableView);
		pTableView->reloadData();

		bRet = true;
	}while(0);

	return bRet;
}



void ListViewLayer::tableCellTouched(TableView* table,TableViewCell* cell)
{
    CCLog("cell touched at index: %i",cell->getIdx());
}

CCSize ListViewLayer::cellSizeForTable(TableView *table)
{
    return CCSizeMake(960,640);
}

TableViewCell* ListViewLayer::tableCellAtIndex(TableView *table,ssize_t idx)
{
    CCString *pString = CCString::createWithFormat("%d",idx);
    TableViewCell *pCell = table->dequeueCell();
    if (!pCell) {
        pCell = new TableViewCell();
        pCell->autorelease();
        CCSprite *pSprite = CCSprite::create("listitem.png");
		pSprite->setScaleY(CCDirector::sharedDirector()->getVisibleSize().height / pSprite->getContentSize().height);
        pSprite->setAnchorPoint(CCPointZero);
		pSprite->setPosition(CCPointZero);
        pCell->addChild(pSprite);

        CCLabelTTF *pLabel = CCLabelTTF::create(pString->getCString(),"Arial",20.0);
        pLabel->setPosition(CCPointZero);
		pLabel->setAnchorPoint(CCPointZero);
        pLabel->setTag(123);
        pCell->addChild(pLabel);
    }
    else
    {
        CCLabelTTF *pLabel = (CCLabelTTF*)pCell->getChildByTag(123);
        pLabel->setString(pString->getCString());
    }


    return pCell;
}

ssize_t ListViewLayer::numberOfCellsInTableView(TableView *table)
{
    return 20;
}


void ListViewLayer::scrollViewDidScroll(ScrollView *view)
{
	CCLog("scrollViewDidScroll: %i",view->getContentOffset());
}

void ListViewLayer::scrollViewDidZoom(ScrollView *view)
{
}
原文链接:https://www.f2er.com/cocos2dx/344893.html

猜你在找的Cocos2d-x相关文章