控件CCScrollView与CCTableView

来源:互联网 发布:淘宝信誉良好怎么提升 编辑:程序博客网 时间:2024/05/20 05:24

控件CCScrollView与CCTableView在层中经常用到,当我们需要上下滑动左右滑动显示内容时,我们就会需要用到他们,不多说,下面直接通过代码讲解。

在层中用到CCScrollView时需要继承CCScrollviewDelegate,以及包含头文件“cocos-ext.h”和命名空间USING_CC_EXT;

<span style="font-size:14px;">class GalleryLayer : public cocos2d::CCLayer ,public CCScrollViewDelegate ,public cocos2d::extension::CCEditBoxDelegate{public:    virtual bool init();      void menuCloseCallback(CCObject* pSender);    CREATE_FUNC(GalleryLayer);public://scrollview滚动的时候会调用void scrollViewDidScroll(CCScrollView* view);</span>
<span style="font-size:14px;">//scrollview缩放的时候会调用void scrollViewDidZoom(CCScrollView* view);virtual void onEnter();<span style="white-space:pre"></span>//周期函数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;};</span>
<span style="font-size:14px;">//类的实现(只是部分代码)</span>
<span style="font-size:14px;"><span style="white-space:pre"></span>   m_pScrollView = CCScrollView::create(CCSizeMake(960, 640), pLayer);<span style="white-space:pre"></span>//设置</span><span style="font-size: 14px; font-family: Arial, Helvetica, sans-serif;">CCScrollView占据的大小</span>
<span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">   m_pScrollView->setContentOffset(CCPointZero);//设置起点</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">   m_pScrollView->setTouchEnabled(false);<span style="white-space:pre"></span></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">   m_pScrollView->setDelegate(this);  //设置代理</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">   m_pScrollView->setDirection(kCCScrollViewDirectionHorizontal);  //设置是横向还是纵向</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">   pLayer->setContentSize(CCSizeMake(960*3, 640));</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">//设置CCScrollview层的大小</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">   this->addChild(m_pScrollView);</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;">void class_name::scrollViewDidScroll(cocos2d::extension::CCScrollView *view){<span style="white-space:pre"></span>CCLOG("scroll");}void class_name::scrollViewDidZoom(cocos2d::extension::CCScrollView *view){<span style="white-space:pre"></span>CCLOG("zoom");}</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;">void class_name::adjustScrollView(float offset){<span style="white-space:pre"></span>CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();<span style="white-space:pre"></span>CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();<span style="white-space:pre"></span>CCSpriteFrameCache *pCache =  CCSpriteFrameCache::sharedSpriteFrameCache();<span style="white-space:pre"></span>CCSprite *pPoint = (CCSprite *)this->getChildByTag(m_nCurPage);<span style="white-space:pre"></span>pPoint->setDisplayFrame(pCache->spriteFrameByName("button_normal.png"));<span style="white-space:pre"></span>if (offset<0)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>m_nCurPage ++;<span style="white-space:pre"></span>}else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>m_nCurPage --;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if (m_nCurPage <1)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>m_nCurPage = 1;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if(m_nCurPage > 3)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>CCLayer *pLayer = ListViewLayer::create();<span style="white-space:pre"></span>CCScene *pScene = CCScene::create();<span style="white-space:pre"></span>pScene->addChild(pLayer);<span style="white-space:pre"></span>CCDirector::sharedDirector()->replaceScene(pScene);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>pPoint = (CCSprite *)this->getChildByTag(m_nCurPage);<span style="white-space:pre"></span>pPoint->setDisplayFrame(pCache->spriteFrameByName("button_selected.png"));<span style="white-space:pre"></span>CCPoint  adjustPos = ccp(origin.x - visibleSize.width * (m_nCurPage-1), 0);<span style="white-space:pre"></span>m_pScrollView->setContentOffset(adjustPos, true);<span style="white-space:pre"></span>}}</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;">//CCTableView<span style="white-space:pre"></span>//#include "cocos-ext.h"   </span>
<span style="font-size:14px;">//类继承public cocos2d::extension::CCTableViewDataSource, public cocos2d::extension::CCTableViewDelegate</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;"><span style="white-space:pre"></span>//by kongming   控件cctableview<span style="white-space:pre"></span>void Drawroomtableview();   //画tableview<span style="white-space:pre"></span>virtual void scrollViewDidScroll(CCScrollView* view);<span style="white-space:pre"></span>virtual void scrollViewDidZoom(CCScrollView* view);<span style="white-space:pre"></span>//处理触摸事件<span style="white-space:pre"></span>virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell);<span style="white-space:pre"></span>//每一项的宽度和高度<span style="white-space:pre"></span>virtual CCSize cellSizeForTable(CCTableView *table);<span style="white-space:pre"></span>//生成列表每一项的内容<span style="white-space:pre"></span>virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx);<span style="white-space:pre"></span>//一共多少项<span style="white-space:pre"></span>virtual unsigned int numberOfCellsInTableView(CCTableView *table);</span>
<span style="font-size:14px;"></span>
//方法实现
void CLobbyMainScreen::Drawroomtableview(){<span style="white-space: pre;"></span>CCSize winsize=CCDirector::sharedDirector()->getWinSize();<span style="white-space: pre;"></span>CCTableView* pTableView=(CCTableView *)this->getChildByTag(TAG_TABLEVIEW_ROOM);<span style="white-space: pre;"></span>if(pTableView==NULL)<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>pTableView = CCTableView::create(this, CCSizeMake(960, 640));//960, 640    960, 234<span style="white-space: pre;"></span>pTableView->setDirection(kCCScrollViewDirectionHorizontal);<span style="white-space: pre;"></span>pTableView->setPosition(ccp(0,115));//CCPointZero0,115<span style="white-space: pre;"></span>pTableView->setDelegate(this);<span style="white-space: pre;"></span>pTableView->setVerticalFillOrder(kCCTableViewFillTopDown);<span style="white-space: pre;"></span>pTableView->setViewSize(CCSizeMake(960,234));<span style="white-space: pre;"></span>g_mainscreen_classPtr->addChild(pTableView,1,TAG_TABLEVIEW_ROOM);<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>pTableView->reloadData();}void CLobbyMainScreen::tableCellTouched(CCTableView* table, CCTableViewCell* cell){<span style="white-space: pre;"></span>int i=cell->getIdx();<span style="white-space: pre;"></span>SelectRoomCallBack(i);}CCSize CLobbyMainScreen::cellSizeForTable(CCTableView *table){<span style="white-space: pre;"></span>return CCSizeMake(230,160);//230,160}CCTableViewCell* CLobbyMainScreen::tableCellAtIndex(CCTableView *table, unsigned int idx){<span style="white-space: pre;"></span>CCTableViewCell *pCell = table->dequeueCell();<span style="white-space: pre;"></span>if (!pCell) {<span style="white-space: pre;"></span>pCell = new CCTableViewCell();<span style="white-space: pre;"></span>pCell->autorelease();<span style="white-space: pre;"></span>//CButton *playermenuitem= CButton::create( LOBBY_MAIN_SCREEN_PLAYER_MSG_BG_PNG, LOBBY_MAIN_SCREEN_PLAYER_MSG_BG_PNG, <span style="white-space: pre;"></span>//NULL, NULL,  NULL, menu_selector(CLobbyMainScreen::PlayerMsgCallBack), 0);<span style="white-space: pre;"></span>m_room_sprite[idx] = CCSprite::create( "gamewindow.png" );<span style="white-space: pre;"></span>m_room_sprite[idx]->setTag(TAG_ENTER_ROOMID+idx);<span style="white-space: pre;"></span>m_room_sprite[idx]->setAnchorPoint(CCPointZero);<span style="white-space: pre;"></span>m_room_sprite[idx]->setPosition(/*CCPointZero*/ccp(80,-70));//-360-70<span style="white-space: pre;"></span>pCell->addChild(m_room_sprite[idx]);<span style="white-space: pre;"></span>drawRoomInformation(idx);<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>else<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>drawRoomInformation(idx);<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>return pCell;}unsigned int CLobbyMainScreen::numberOfCellsInTableView(CCTableView *table){<span style="white-space: pre;"></span>return 3;}void CLobbyMainScreen::scrollViewDidScroll(CCScrollView *view){}void CLobbyMainScreen::scrollViewDidZoom(CCScrollView *view){}




0 0
原创粉丝点击