界面之间的滑动

.h文件:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
//#include "uiscene.h"
#include "UIPageView.h"
#include "UIText.h"
class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();
	//HelloWorld();
	//~HelloWorld();

	void pageViewEvent(cocos2d::Ref* sender, cocos2d::ui::PageView::EventType type);
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();
protected:
	cocos2d::ui::Text* _displayValueLabel;
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

.cpp文件:
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "UILayout.h"
USING_NS_CC;
using namespace cocos2d::ui;
using namespace cocostudio::timeline;

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();
	
		// Create the page view
		PageView* pageView = PageView::create();
		pageView->setContentSize(Size(240.0f, 130.0f));
		pageView->setPosition(Vec2(visibleSize.width / 2-80.f,
			visibleSize.height / 2-50.f));
	
		pageView->removeAllPages();

		int pageCount = 4;
		for (int i = 0; i < pageCount; ++i)
		{
			Layout* layout = Layout::create();
			layout->setContentSize(Size(240.0f, 130.0f));

			ImageView* imageView = ImageView::create("scrollviewbg.png");
			imageView->setScale9Enabled(true);
			imageView->setContentSize(Size(240, 130));
			imageView->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
			layout->addChild(imageView);

			Text* label = Text::create(StringUtils::format("page %d", (i + 1)), "fonts/Marker Felt.ttf", 30);
			label->setColor(Color3B(192, 192, 192));
			label->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
			layout->addChild(label);

			pageView->insertPage(layout, i);
		}

		pageView->removePageAtIndex(0);
		pageView->scrollToPage(pageCount - 2);

		pageView->addEventListener(CC_CALLBACK_2(HelloWorld::pageViewEvent, this));

		addChild(pageView);

		return true;
	}
	
void HelloWorld::pageViewEvent(Ref *pSender, PageView::EventType type)
{
	switch (type)
	{
	case PageView::EventType::TURNING:
	{
		PageView* pageView = dynamic_cast<PageView*>(pSender);

		//_displayValueLabel->setString(StringUtils::format("page = %ld", pageView->getCurPageIndex() + 1));
	}
	break;

	default:
		break;
	}
}



效果图:



原文地址:https://www.cnblogs.com/Anzhongliu/p/6091901.html