界面的滑动01(cocos3.8)

前端之家收集整理的这篇文章主要介绍了界面的滑动01(cocos3.8)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

.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 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
}



效果


原文链接:https://www.f2er.com/cocos2dx/341034.html

猜你在找的Cocos2d-x相关文章