#include "cocos2d.h" #include "extensions/cocos-ext.h"//包含TableView的头文件 #include "ItemAdapter.h"//适配类 USING_NS_CC; //事件回调函数 typedef std::function<void(Ref*,cocos2d::ui::Widget::TouchEventType)> menuCallBack; //继承类 class MenuTableView : public cocos2d::Layer, public cocos2d::extension::TableViewDataSource, public cocos2d::extension::TableViewDelegate { public: CREATE_FUNC(MenuTableView); 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::Size tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx);//Cell大小 virtual cocos2d::extension::TableViewCell* tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx);//添加Cell virtual ssize_t numberOfCellsInTableView(cocos2d::extension::TableView *table);//设置Cell个数 void touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type);//触摸事件 void addMenuTouchEvent(const menuCallBack& callback);//回调函数 Size winSize; cocos2d::Ref* _pSender; cocos2d::ui::Widget::TouchEventType _type; menuCallBack _menuCallBack; std::vector<std::string> item; std::vector<std::string> itemIcon; };
#include "MenuTableView.h" #include "ui/CocosGUI.h" #include "HomeLayer.h" USING_NS_CC; USING_NS_CC_EXT;//包含cocos-ext.h头文件的声明 using namespace ui; bool MenuTableView::init() { bool bRet = false; do { CC_BREAK_IF(!Layer::init()); log("MenuTableView"); //item must be defined first//设置Item名称和背景 item.push_back("进度"); item.push_back("同比"); item.push_back("环比"); item.push_back("产品线"); item.push_back("排名"); itemIcon.push_back("home_list_bg_jindu.png"); itemIcon.push_back("home_list_bg_tongbi.png"); itemIcon.push_back("home_list_bg_huanbi.png"); itemIcon.push_back("home_list_bg_chanpinxian-.png"); itemIcon.push_back("home_list_bg_paiming-.png"); winSize = Director::getInstance()->getWinSize(); //创建TableView对象 TableView* tableView = TableView::create(this,Size(winSize.width,winSize.height*6/7)); tableView->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);//设置TableView方向 tableView->ignoreAnchorPointForPosition(false); tableView->setAnchorPoint(Vec2::ZERO);//设置锚点 tableView->setPosition(Vec2::ZERO);//设置位置 tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);//设置TableView的属性 tableView->setDelegate(this);//设置委托 this->addChild(tableView); tableView->reloadData();//更新数据 this->setTouchEnabled(true);//可触摸 bRet = true; }while(0); return bRet; } //Item触摸事件 void MenuTableView::tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell) { log("cell touched at index %zd", cell->getIdx()); } //设置Cell大小 Size MenuTableView::tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx) { return Size(winSize.width,winSize.height/6); } //添加Cell TableViewCell* MenuTableView::tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx) { TableViewCell* cell = table->dequeueCell(); if(!cell) { cell = new TableViewCell(); cell->autorelease(); } cell->removeAllChildrenWithCleanup(true); log("idx = %zd",idx); auto menu = ItemAdapter::create();//适配类 menu->itemIcon->loadTexture(itemIcon.at(idx));//添加Icon menu->itemName->setString(item.at(idx));//添加Text menu->touchBtn->setTag(10+idx); menu->setContentSize(Size(winSize.width,winSize.height/8));//设置视图大小 log("menu.width = %lf,menu.height = %lf",menu->getContentSize().width,menu->getContentSize().height); menu->setSize(Size(winSize.width,winSize.height/8));//设置大小 menu->setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);//设置布局类型 menu->ignoreAnchorPointForPosition(false); menu->setAnchorPoint(Vec2::ZERO);//设置锚点 menu->touchBtn->addTouchEventListener(CC_CALLBACK_2(MenuTableView::touchEvent, this));//设置Item触摸事件 cell->addChild(menu); return cell; } //设置Cell个数 ssize_t MenuTableView::numberOfCellsInTableView(cocos2d::extension::TableView *table) { return 5; } void MenuTableView::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)//回调函数,执行HelloWorld.cpp的touch事件 { if(_menuCallBack) _menuCallBack(pSender,type); } //给_menuCallBack赋值,callback是HelloWorld.cpp中的touchEvent事件 void MenuTableView::addMenuTouchEvent(const menuCallBack &callback) { _menuCallBack = callback; }
#include "cocos2d.h" #include "ui/CocosGUI.h" USING_NS_CC; using namespace ui; class ItemAdapter : public Layout { public: CREATE_FUNC(ItemAdapter); virtual bool init(); void touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type); ui::Text* itemName; ui::ImageView* itemIcon; ui::ImageView* bgImage; ui::Button* touchBtn; };
#include "ItemAdapter.h" #include "VisibleRect.h" bool ItemAdapter::init() { bool bRet = false; do { CC_BREAK_IF(!Layout::init()); auto winSize = Director::getInstance()->getWinSize(); itemIcon = ui::ImageView::create();//Icon itemIcon->setScaleY(VisibleRect::getImageScaleY()); itemIcon->setScaleX(VisibleRect::getImageScale()); itemIcon->ignoreAnchorPointForPosition(false); itemIcon->setAnchorPoint(Vec2(0.5,0.5)); // itemIcon->setPosition(VisibleRect::center()); auto rIcon = RelativeLayoutParameter::create(); rIcon->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT); itemIcon->setLayoutParameter(rIcon); addChild(itemIcon); itemName = ui::Text::create();//Text itemName->ignoreAnchorPointForPosition(false); itemName->setAnchorPoint(Vec2(0.5,0.5)); itemName->setFontSize(80 * VisibleRect::getImageScale()); itemName->setColor(Color3B(128,128,128)); auto rprogress = RelativeLayoutParameter::create(); rprogress->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL); rprogress->setMargin(Margin(90*VisibleRect::getImageScale(),0,0,0)); itemName->setLayoutParameter(rprogress); addChild(itemName); bgImage = ui::ImageView::create("home_list_jiantou.png"); bgImage->setScaleY(VisibleRect::getImageScaleY()); bgImage->setScaleX(VisibleRect::getImageScale()); bgImage->ignoreAnchorPointForPosition(false); // into->setTag(PrograssBtn_Tag+i); bgImage->setAnchorPoint(Vec2(0.5,0.5)); auto rinto = RelativeLayoutParameter::create(); rinto->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL); rinto->setMargin(Margin(0,0,90*VisibleRect::getImageScale(),0)); bgImage->setLayoutParameter(rinto); addChild(bgImage,0); auto stencil = ui::ImageView::create("home_list_bg_jindu.png"); touchBtn = Button::create("touming.png"); log("itemsize.w = %lf,itemsize.h = %lf",stencil->getSize().width,stencil->getSize().height ); log("touchBtn.x = %lf,touchBtn.y = %lf",touchBtn->getSize().width,touchBtn->getSize().height ); auto width = stencil->getSize().width * VisibleRect::getImageScale(); auto height = stencil->getSize().height * VisibleRect::getImageScaleY(); touchBtn->setScaleX(width / touchBtn->getSize().width); touchBtn->setScaleY(height / touchBtn->getSize().height); touchBtn->ignoreAnchorPointForPosition(false); // into->setTag(PrograssBtn_Tag+i); touchBtn->setAnchorPoint(Vec2(0.5,0.5)); // touchBtn->setEnabled(true); auto rtouchBtn = RelativeLayoutParameter::create(); rtouchBtn->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT); rtouchBtn->setMargin(Margin(0,0,0,0)); touchBtn->setLayoutParameter(rtouchBtn); addChild(touchBtn,1); bRet = true; }while(0); return bRet; }HelloWorld.cpp中添加如下代码
auto menuTableView = MenuListView::create(); menuTableView->ignoreAnchorPointForPosition(false); menuTableView->setAnchorPoint(Vec2::ZERO); menuTableView->setContentSize(Size(winSize.width, winSize.height/7*6)); menuTableView->setPosition(Vec2::ZERO); addChild(menuTableView); menuTableView->addMenuTouchEvent(CC_CALLBACK_2(HomeWorld::touchEvent,this));
void HomeLayer::touchEvent(Ref *pSender,cocos2d::ui::Widget::TouchEventType type) { auto* btn = dynamic_cast<ui::ImageView*>( pSender); switch (type) { case cocos2d::ui::Widget::TouchEventType::ENDED: { switch (btn->getTag()) { case DayReport_Tag: { log("1"); } break; case PrograssBtn_Tag: { log("2"); } break; case SameBtnCompare_Tag: { <pre name="code" class="cpp"> log("3");} break; case RingCompare_Tag: {
log("4");} break; case ProductLine_Tag: {
log("5");} break; default: break; } } break; default: break; }}
原文地址:http://blog.csdn.net/yuxikuo_1/article/details/38870525