界面的滑动01(cocos3.8)
2015-10-25 03:16
435 查看
.h文件:
.cpp文件:
效果:
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "ui/UIScrollView.h" class HelloWorld : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); protected: // cocos2d::ui::Text* _displayValueLabel; std::vector<std::string> _array; // implement the "static create()" method manually CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
.cpp文件:
#include "HelloWorldScene.h" #include "ui/CocosGUI.h" #include "cocostudio/CocoStudio.h" USING_NS_CC; using namespace cocos2d::ui; const char* font_UIListViewTest = "fonts/Marker Felt.ttf"; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); /////////// // create list view ex data for (int i = 0; i < 20; ++i) { std::string ccstr = StringUtils::format("listview_item_%d", i); _array.push_back(ccstr); } Button* default_button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png"); default_button->setName("Title Button"); ListView* listView = ListView::create(); listView->setBounceEnabled(true);//弹性滑动 //listView->setClippingEnabled(true); //listView->setClippingType(ui::Layout::ClippingType::SCISSOR); listView->setBackGroundImage("cocosui/green_edit.png"); listView->setScrollBarPositionFromCorner(Vec2(7, 7)); listView->setBackGroundImageScale9Enabled(true); listView->setContentSize(Size(480, 260));//(240, 130) listView->setAnchorPoint(Vec2(0.50f, 0.50f)); listView->setPosition(Vec2((visibleSize.width) / 2.0f,//+// - backgroundSize.width //(listView->getContentSize().width) / 2.0f,//backgroundSize.width - (visibleSize.height) / 2.0f));// +// - backgroundSize.height //(listView->getContentSize().height) / 2.0f));//backgroundSize.height - Layout* default_item = Layout::create(); default_item->setTouchEnabled(true); default_item->setContentSize(default_button->getContentSize()); default_button->setPosition(Vec2(default_item->getContentSize().width / 2.0f, default_item->getContentSize().height / 2.0f)); default_item->addChild(default_button); this->addChild(listView); // Create the list view ex /*ListView* listView = ListView::create(); // set list view ex direction //listView->setDirection(ui::ScrollView::Direction::VERTICAL); //listView->setBounceEnabled(true);//弹性滑动 listView->setBackGroundImage("cocosui/green_edit.png"); listView->setBackGroundImageScale9Enabled(true); listView->setContentSize(Size(240, 130)); listView->setPosition(Vec2((visibleSize.width) / 2.0f +// - backgroundSize.width (listView->getContentSize().width) / 2.0f,//backgroundSize.width - (visibleSize.height) / 2.0f +// - backgroundSize.height (listView->getContentSize().height) / 2.0f));//backgroundSize.height - //listView->addEventListener((ui::ListView::ccListViewCallback)CC_CALLBACK_2(HelloWorld::selectedItemEvent, this)); //listView->addEventListener((ui::ListView::ccScrollViewCallback)CC_CALLBACK_2(HelloWorld::selectedItemEventScrollView, this)); //listView->setScrollBarPositionFromCorner(Vec2(7, 7)); this->addChild(listView); // create model Button* default_button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png"); default_button->setName("Title Button"); Layout* default_item = Layout::create(); //default_item->setTouchEnabled(true); default_item->setContentSize(default_button->getContentSize()); default_button->setPosition(Vec2(default_item->getContentSize().width / 2.0f, default_item->getContentSize().height / 2.0f)); default_item->addChild(default_button); // set model listView->setItemModel(default_item); */ // add default item ssize_t count = _array.size(); for (int i = 0; i < count / 4; ++i) { //listView->pushBackDefaultItem(); } // insert default item for (int i = 0; i < count / 4; ++i) { //listView->insertDefaultItem(0); } listView->removeAllChildren(); Sprite* testSprite = Sprite::create("cocosui/backtotoppressed.png"); testSprite->setPosition(Vec2(200, 200)); listView->addChild(testSprite); // add custom item /* for (int i = 0; i < count / 4; ++i) { Button* custom_button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); custom_button->setName("Title Button"); custom_button->setScale9Enabled(true); custom_button->setContentSize(default_button->getContentSize()); Layout *custom_item = Layout::create(); custom_item->setContentSize(custom_button->getContentSize()); custom_button->setPosition(Vec2(custom_item->getContentSize().width / 2.0f, custom_item->getContentSize().height / 2.0f)); custom_item->addChild(custom_button); listView->addChild(custom_item); }*/ // insert custom item Vector<Widget*>& items = listView->getItems(); ssize_t items_count = items.size(); for (int i = 0; i < count / 4; ++i) { Button* custom_button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); custom_button->setName("Title Button"); custom_button->setScale9Enabled(true); custom_button->setContentSize(default_button->getContentSize()); Layout *custom_item = Layout::create(); custom_item->setContentSize(custom_button->getContentSize()); custom_button->setPosition(Vec2(custom_item->getContentSize().width / 2.0f, custom_item->getContentSize().height / 2.0f)); custom_item->addChild(custom_button); custom_item->setTag(1); listView->insertCustomItem(custom_item, items_count); } // set item data items_count = items.size(); for (int i = 0; i < items_count; ++i) { Widget* item = listView->getItem(i); Button* button = static_cast<Button*>(item->getChildByName("Title Button")); ssize_t index = listView->getIndex(item); button->setTitleText(_array[index]); } // remove last item //listView->removeChildByTag(1); // remove item by index //items_count = items.size(); //listView->removeItem(items_count - 1); // set all items layout gravity //listView->setGravity(ListView::Gravity::CENTER_VERTICAL); // set items margin listView->setItemsMargin(2.0f); return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
效果:
相关文章推荐
- 初学cocos2dx-lua之Mac配置sumblime Text3 编译lua
- cocos2d-lua集成Mob-SMSSDK-for-android
- [Cocos2d塔防游戏开发]Cocos2dx-3.X完成塔防游戏《王国保卫战》--防御塔(三)之初级炮塔
- Cocos2d-x 3.8.1学习之路:(2)从windows到android
- Cocos2D-x内存管理的原则
- Cocos2d-JS使用log方法控制台无法打印出结果
- Cocos2d-JS屏幕适配
- Cocos2d-x下Lua调用自定义C++类和函数的最佳实践
- android台 cocos2d-x 翻阅相册数据
- cocos2d-x项目目录梳理
- cocos-2d HelloWorld项目运行报错解决
- Protobuf2.6集成到cocos2dx(Android和ios平台)
- cocos2d-x-3.5 android 编译
- Cocos2d入门--3--小球运动
- Cocos2d-X采用CCScrollView创建滚动视图
- cocos2d平台游戏移到windows平台运行
- cocos2dx之EditBox
- cocos2dx-------------------------------------Speed && Follow
- cocos2d-x引擎目录结构分析
- Cocos2d入门--2--三角函数的应用