#include "GalleryLayer.h"
#include "ListViewLayer.h"
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();
CCLayer *pLayer = CCLayer::create();
char helpstr[30] = {0};
for (int i = 1; i <= 3; ++ i)
{
memset(helpstr, 0, sizeof(helpstr));
sprintf(helpstr,"bg_%02d.png",i);
CCSprite *pSprite = CCSprite::create(helpstr);
pSprite->setPosition(ccp(visibleSize.width * (i-0.5f), visibleSize.height / 2));
pLayer->addChild(pSprite);
}
m_pScrollView = CCScrollView::create(CCSizeMake(960, 640), pLayer);
m_pScrollView->setContentOffset(CCPointZero);
m_pScrollView->setTouchEnabled(false);
m_pScrollView->setDelegate(this);
m_pScrollView->setDirection(kCCScrollViewDirectionHorizontal);
pLayer->setContentSize(CCSizeMake(960*3, 640));
this->addChild(m_pScrollView);
CCSpriteFrameCache *pCache = CCSpriteFrameCache::sharedSpriteFrameCache();
pCache->addSpriteFrame(CCSpriteFrame::create("button_normal.png",CCRectMake(0, 0, 64, 64)),"button_normal.png");
pCache->addSpriteFrame(CCSpriteFrame::create("button_selected.png",CCRectMake(0, 0, 64, 64)),"button_selected.png");
for (int i = 1; i <= 3; ++ 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);
return bRet;
}
void GalleryLayer::menuCloseCallback(CCObject* pSender)
{
}
void GalleryLayer::scrollViewDidScroll(cocos2d::extension::CCScrollView *view)
{
CCLOG("scroll");
}
void GalleryLayer::scrollViewDidZoom(cocos2d::extension::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(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
CCPoint endPoint = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
float distance = endPoint.x - m_touchPoint.x;
if(fabs(distance) > 50)
{
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);
pPoint->setDisplayFrame(pCache->spriteFrameByName("button_normal.png"));
if (offset<0)
{
m_nCurPage ++;
}else
{
m_nCurPage --;
}
if (m_nCurPage <1)
{
m_nCurPage = 1;
}
if(m_nCurPage > 3)
{
CCLayer *pLayer = ListViewLayer::create();
CCScene *pScene = CCScene::create();
pScene->addChild(pLayer);
CCDirector::sharedDirector()->replaceScene(pScene);
}
else
{
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->setContentOffset(adjustPos, true);
}
}
cocos2d-x CCScrollView和CCTableView的使用 实例源码
5星 · 超过95%的资源 需积分: 9 170 浏览量
2013-01-08
22:59:12
上传
评论 15
收藏 1.21MB RAR 举报
zhoujianghai
- 粉丝: 41
- 资源: 55
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页