CCScroview用法
来源:互联网 发布:淘宝上买鞋子是正品吗 编辑:程序博客网 时间:2024/05/22 12:46
代码如下:
#ifndef GALLERY_LAYER_H#define GALLERY_LAYER_H#include "cocos2d.h"#include "SimpleAudioEngine.h"#include "cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT;class GalleryLayer : public cocos2d::CCLayer ,public CCScrollViewDelegate{public: static CCScene* scene(); virtual bool init(); void menuCloseCallback(CCObject* pSender); void menuLeftCallback(CCObject* pSender); void menuRightCallback(CCObject* pSender); CREATE_FUNC(GalleryLayer); 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_pScrollView; CCPoint m_touchPoint; int m_nCurPage;};#endif
#include "GalleryLayer.h"#define PICNUM 5CCScene* GalleryLayer::scene(){ CCScene* scene = CCScene::create(); CCLayer* layer = GalleryLayer::create(); scene->addChild(layer); return scene;}bool GalleryLayer::init(){ bool bRet = false; do { CC_BREAK_IF( !CCLayer::init() ); m_nCurPage = 1; CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png", this,menu_selector(GalleryLayer::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenuItemImage *leftItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png", this,menu_selector(GalleryLayer::menuLeftCallback)); leftItem->setPosition(ccp(leftItem->getContentSize().width , winSize.height/2)); CCMenuItemImage *rightItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png", this,menu_selector(GalleryLayer::menuRightCallback)); rightItem->setPosition(ccp(winSize.width-rightItem->getContentSize().width , winSize.height/2)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, leftItem, rightItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); CCLayer *pLayer = CCLayer::create(); char helpstr[30] = {0}; for (int i = 1; i <= PICNUM; ++ i) { memset(helpstr, 0, sizeof(helpstr)); sprintf(helpstr,"%d.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 <= PICNUM; ++ 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::menuLeftCallback(CCObject* pSender){ adjustScrollView(90);}void GalleryLayer::menuRightCallback(CCObject* pSender){ adjustScrollView(-90);}void GalleryLayer::menuCloseCallback(CCObject* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");#else CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0);#endif#endif}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) > 100) { adjustScrollView(distance); }}void GalleryLayer::adjustScrollView(float offset){ CCLOG("=offset=%.3f",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 > 5) { m_nCurPage = 5; } CCLOG("=m_nCurPage=%d",m_nCurPage); 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);}
0 0
- CCScroview用法
- 用法
- ,, 用法
- 用法
- 用法
- #用法
- ??用法
- !!用法
- CustDialog用法 SimpleAdapter 用法
- hibernate_Restrictions用法 hibernate_Restrictions用法
- extern用法+assert用法
- getParameterValues用法
- rpm用法
- Log4j用法
- NameSpace用法
- sp_executesql 用法
- ShellExecute用法
- getParameterValues用法
- Linux与BSD中TCP协议栈实现比较
- iOS中UISearchBar(搜索框)使用总结
- 第四周--猴子选大王
- C++ 的swap手法
- Week1-5Why is NLP hard?
- CCScroview用法
- Knockout应用开发指南 第七章:Mapping插件
- 第七周 项目2 建立链队算法库
- Spring源码分析
- 针对虚幻3引擎渲染底层的效率优化
- c# - string to byte[] and vice versa
- 16.1 目标跟踪的项目
- hdu 2504 又见GCD
- 有价值的参考网页和博客