直接上代码:
HelloWorldScene.cpp
#include "HelloWorldScene.h"USING_NS_CC;#define COIN_WIDTH 212 //coin 图片宽度#define COIN_GAP 100 //间隔#define COIN_COUNT 11 //个数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(); //XKPageVIEw::create(Size size,XKPageVIEwDelegate *delegate) pageVIEw = XKPageVIEw::create(Size(visibleSize.wIDth,COIN_WIDTH),this); pageVIEw -> setDirection(ScrollVIEw::Direction::HORIZONTAL); //container 定位在屏幕中间 pageVIEw -> setposition(Point((visibleSize.wIDth - COIN_WIDTH) * 0.5,(visibleSize.height - COIN_WIDTH) * 0.5)); addPages(); //设置裁切为false,这样layer 溢出pageVIEw的Size还能显示 pageVIEw -> setClipPingToBounds(false); this -> addChild(pageVIEw); //设置pageVIEw 自动调整时的callback,可以在这里写你要的功能 pageVIEw -> adjustCallback = [](XKPageVIEw *pageVIEw){ FiniteTimeAction *fadeIn = Fadeto::create(0.15,255); FiniteTimeAction *fadeOut = Fadeto::create(0.15,255 * 0.3); FiniteTimeAction *scaletoBig = Scaleto::create(0.15,1.5f); FiniteTimeAction *scaletoSmall = Scaleto::create(0.15,1.0f); Spawn *spawnIn = Spawn::createWithTwoActions(fadeIn,scaletoBig); Spawn *spawnOut = Spawn::createWithTwoActions(fadeOut,scaletoSmall); int current = pageVIEw -> getCurrentIndex(); auto sprite = (Sprite*) pageVIEw -> getPageAtIndex(current); sprite -> runAction(spawnIn); sprite = (Sprite *)pageVIEw -> getPageAtIndex(current - 1); if (sprite !=NulL){ sprite -> runAction(spawnOut); } sprite = (Sprite *)pageVIEw -> getPageAtIndex(current + 1); if (sprite !=NulL){ sprite -> runAction(spawnOut -> clone()); } }; return true;}voID HelloWorld::addPages(){ Size coinSize = Sprite::create("coin.png") -> getContentSize(); //11个layer 加到layer 上 for (int i = 0; i < COIN_COUNT; i++) { auto sprite = Sprite::create("coin.png"); sprite -> setposition(coinSize.wIDth * 0.5,coinSize.height * 0.5); std::string str = StringUtils::format("%d",i); Label *label = Label::createWithSystemFont(str,"Arial",60); label -> setTextcolor(color4B(0,255)); Size size = sprite -> getContentSize(); label -> setposition(size.wIDth * 0.5,size.height * 0.5); sprite -> addChild(label); //注意这里使用的时XKPageVIEw 的addPage,不是addChild pageVIEw -> addPage(sprite); }}Size HelloWorld::sizeforPerPage(){ //Delegate 的东西,返回每个Page 的Size return Size(COIN_WIDTH + COIN_GAP,COIN_WIDTH);}voID HelloWorld::pageVIEwDIDScroll(XKPageVIEw *pageVIEw){ //监听滚动时间,可以再这里写滚动时候要添加的代码,比如缩放~ Size visibleSize = Director::getInstance() -> getVisibleSize(); float mIDX = visibleSize.wIDth / 2; float offsetX = pageVIEw -> getContentOffset().x; float tmp = COIN_WIDTH / 2.0f; float scale = 1.5; scale = scale - 1; for (int i = 0; i < COIN_COUNT; i++) { auto sprite = (Sprite *) pageVIEw -> getPageAtIndex(i); float positionX = sprite -> getpositionX(); //转换成相对屏幕坐标 float endX = positionX + offsetX + mIDX - tmp; if (0 < endX && endX <= mIDX) { float x = endX / mIDX * scale + 1; sprite -> setScale(x); x = (endX / mIDX * 0.7 + 0.3) * 255; sprite -> setopacity(x); }else if(endX > mIDX && endX < visibleSize.wIDth){ float tmp2 = endX - mIDX; tmp2 = mIDX - tmp2; float x = tmp2 / mIDX * scale + 1; sprite -> setScale(x); x = (tmp2 / mIDX * 0.7 + 0.3) * 255; sprite -> setopacity(x); }else { sprite -> setScale(1.0f); sprite -> setopacity(255 * 0.3); } }}voID HelloWorld::onEnter(){ Layer::onEnter(); pageVIEw -> adjustCallback(pageVIEw);}
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "extensions/cocos-ext.h"#include "XKPageVIEw.h"USING_NS_CC_EXT;class HelloWorld : public cocos2d::Layer,public XKPageVIEwDelegate{public: // there's no 'ID' in cpp,so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'ID' in cocos2d-iphone virtual bool init(); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); // PageVIEwDelegate virtual Size sizeforPerPage(); virtual voID pageVIEwDIDScroll(XKPageVIEw *pageVIEw);private: XKPageVIEw *pageVIEw; voID addPages();public: virtual voID onEnter();};#endif // __HELLOWORLD_SCENE_H__
XKPageVIEw.h
//// XKPageVIEw.h// XKPageVIEw//// Created by Joueu on 14-11-26.////#ifndef __XKPageVIEw__XKPageVIEw__#define __XKPageVIEw__XKPageVIEw__#include "cocos2d.h"#include "extensions/cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT;class XKPageVIEw;class XKPageVIEwDelegate{public: virtual ~XKPageVIEwDelegate(){}; XKPageVIEwDelegate(){}; //获取Page大小 virtual Size sizeforPerPage() = 0; //滚动回调 virtual voID pageVIEwDIDScroll(XKPageVIEw *pageVIEw){};};class XKPageVIEw:public ScrollVIEw{public: static XKPageVIEw *create(Size size,XKPageVIEwDelegate *delegate); virtual bool init(Size size,XKPageVIEwDelegate *delegate);public: voID setPageSize(Size size); virtual voID setContentOffsetInDuration(Vec2 offset,float dt); virtual voID setContentOffset(Vec2 offset);private: voID performedAnimatedScroll(float dt); int current_index; float current_offset; //调整offset 的函数 voID adjust(float offset); Size pageSize; CC_SYNTHESIZE(XKPageVIEwDelegate *,_delegate,Delegate);public: int pageCount; voID addPage(Node *node); Node *getPageAtIndex(int index); int getCurrentIndex(); //滚动后调整回调 std::function<voID(XKPageVIEw *)> adjustCallback;};#endif /* defined(__XKPageVIEw__XKPageVIEw__) */
//// XKPageVIEw.cpp// XKPageVIEw//// Created by Joueu on 14-11-26.////#include "XKPageVIEw.h"#define XKPAGEVIEW_TAG 10086XKPageVIEw *XKPageVIEw::create(Size size,XKPageVIEwDelegate *delegate){ XKPageVIEw *page = new XKPageVIEw(); if (page && page -> init(size,delegate)) { page -> autorelease(); }else { CC_SAFE_RELEASE(page); } return page;}bool XKPageVIEw::init(Size size,XKPageVIEwDelegate *delegate){ if (!ScrollVIEw::initWithVIEwSize(size)) { return false; } //必须有delegate,否则断掉 CCASSERT(delegate,"delegate should not be NulL!"); setDelegate(delegate); if (_delegate) { //获取page的大小 pageSize = _delegate -> sizeforPerPage(); } //init Data pageCount = 0; current_index = 0; this -> settouchEnabled(false); auto Listener = EventListenertouchOneByOne::create(); Listener -> ontouchBegan = [&](touch *touch,Event *event){ _dragging = false; this -> scheduleUpdate(); if (_direction == ScrollVIEw::Direction::HORIZONTAL) { current_offset = this -> getContentOffset().x; }else { current_offset = this -> getContentOffset().y; } return true; }; Listener -> ontouchmoved = [&](touch *touch,Event *event){ float start,end; if (_direction == ScrollVIEw::Direction::HORIZONTAL) { start = touch -> getStartLocation().x; end = touch -> getLocation().x; }else { start = touch -> getStartLocation().y; end = touch -> getLocation().y; } float offset = end - start; // * 2的作用是调节滚动速度,需要调滑动速度的 可以改这个值 if (_direction == ScrollVIEw::Direction::HORIZONTAL) this -> setContentOffset(Vec2(current_offset + offset * 2,0)); else this -> setContentOffset(Vec2(0,current_offset + offset * 2)); }; Listener -> ontouchended = [&](touch *touch,Event *event){ float start = current_offset,end; if (_direction == ScrollVIEw::Direction::HORIZONTAL) { end = this -> getContentOffset().x; }else { end = this -> getContentOffset().y; } float offset = end - start; this -> adjust(offset); _dragging = true; }; Director::getInstance() -> getEventdispatcher() -> addEventListenerWithSceneGraPHPriority(Listener,this); return true;}voID XKPageVIEw::adjust(float offset){ Vec2 vec; float xOrY; if (_direction == ScrollVIEw::Direction::HORIZONTAL) { vec = Vec2(-( current_index * (pageSize.wIDth)),0); xOrY = pageSize.wIDth; }else { vec = Vec2(0,-( current_index * (pageSize.height))); xOrY = pageSize.height; } //小于50回到原来位置 if (abs(offset) < 50){ this -> setContentOffsetInDuration(vec,0.15f); return; } int i = abs(offset / (xOrY)) + 1; if (offset < 0) { current_index += i; }else { current_index -= i; } if (current_index < 0) { current_index = 0; }else if(current_index > 10){ current_index = 10; } if (_direction == ScrollVIEw::Direction::HORIZONTAL) { vec = Vec2(-( current_index * (pageSize.wIDth)),0); }else { vec = Vec2(0,-( current_index * (pageSize.height))); } this -> adjustCallback(this); this -> setContentOffsetInDuration(vec,0.15f);}voID XKPageVIEw::setContentOffset(Vec2 offset){ ScrollVIEw::setContentOffset(offset); if (_delegate != nullptr) { _delegate -> pageVIEwDIDScroll(this); }}voID XKPageVIEw::setContentOffsetInDuration(Vec2 offset,float dt){ ScrollVIEw::setContentOffsetInDuration(offset,dt); this->schedule(schedule_selector(XKPageVIEw::performedAnimatedScroll));}voID XKPageVIEw::performedAnimatedScroll(float dt){ if (_dragging) { this->unschedule(schedule_selector(XKPageVIEw::performedAnimatedScroll)); this -> unscheduleUpdate(); return; } if (_delegate != nullptr) { _delegate -> pageVIEwDIDScroll(this); }}voID XKPageVIEw::addPage(Node *node){ if (_direction == ScrollVIEw::Direction::HORIZONTAL) { node -> setposition(Point(pageCount * pageSize.wIDth + node -> getpositionX(),node -> getpositionY())); this -> setContentSize(Size((pageCount + 1) * pageSize.wIDth,pageSize.height)); }else { node -> setposition(Point(node -> getpositionX(),pageCount * pageSize.height + node -> getpositionY())); this -> setContentSize(Size(pageSize.wIDth,(pageCount + 1) *pageSize.height)); } node -> setTag(pageCount + XKPAGEVIEW_TAG); _container -> addChild(node); pageCount ++; }Node *XKPageVIEw::getPageAtIndex(int index){ if (index < pageCount && index >= 0) { return _container -> getChildByTag(index + XKPAGEVIEW_TAG); } return NulL;}int XKPageVIEw::getCurrentIndex(){ return current_index;}
文章转自: http://helkyle.tk/cocos-2dx-3xPageView/ 总结
以上是内存溢出为你收集整理的使用cocos-2dx 3.x 封装的PageView全部内容,希望文章能够帮你解决使用cocos-2dx 3.x 封装的PageView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)