.h文件:
#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 instancebool 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);//d性滑动 //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);//d性滑动 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}
总结
以上是内存溢出为你收集整理的界面的滑动01(cocos3.8)全部内容,希望文章能够帮你解决界面的滑动01(cocos3.8)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)