PageView with cocos-2dx 3.x encapsulation, cocos-2dxpageview

Source: Internet
Author: User

PageView with cocos-2dx 3.x encapsulation, cocos-2dxpageview

Reprinted please indicate the source:


  • Http://helkyle.tk/cocos-2dx-3xPageView)
  • Http://blog.csdn.net/joueu/article/details/41532763

Previously, using PageView seems to be a little difficult to use, so I cannot achieve the functions I want to implement, and I don't want to modify the source code. Recently, it took me half a day to get rid of it and name it XKPageView ~
Not much nonsense. Check the display effect first.

Because the video is recorded on the simulator, it seems that there will be some cards... the real machine will not be tested. (Why is the number 5 so big ?) Check the code...


I will directly go to the Code, and there are comments in the Code. Some simple things will not be too long-winded. If you have any questions, leave a message and ask me ~ , There may be a lot of bugs because there is no time to test so much...


PageView. h

// XKPageView. h // XKPageView /// Created by Joueu on 14-11-26. /// # ifndef _ XKPageView __# define _ XKPageView __# include "cocos2d. h "# include" cocos-ext.h "USING_NS_CC; USING_NS_CC_EXT; class XKPageView; class XKPageViewDelegate {public: virtual ~ Aggregate () {}; XKPageViewDelegate () {}; 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 Size, XKPageViewDelegate * delegate); public: void setPageSize (size Size ); virtual void setContentOffsetInDuration (Vec2 offset, float dt); virtual void setContentOffset (Vec2 offset); private: void merge medanimatedscroll (float dt); int current_index; float current_offset; // adjust the offset function void adjust (float offset); Size pageSize; CC_SYNTHESIZE (XKPageViewDelegate *, _ delegate, Delegate); public: int pageCount; void addPage (Node * node ); node * getPageAtIndex (int index) ;};# endif/* defined (_ XKPageView __)*/


XKPageView. cpp
//// XKPageView. cpp // XKPageView /// Created by Joueu on 14-11-26. //// # include "XKPageView. h "# define XKPAGEVIEW_TAG 10086 XKPageView * 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 * delegat E) {if (! ScrollView: initWithViewSize (size) {return false;} // delegate is required; otherwise, CCASSERT (delegate, "delegate shocould not be NULL! "); SetDelegate (delegate); if (_ delegate) {// get the page size 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; if (_ direction = ScrollView: Direction: HORIZONTAL) {current_offset = this-> getContentOff Set (). 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; // For * 2 The function is to adjust the rolling speed. if you need to adjust the sliding speed, you can change this value 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;} // return to the original position if (abs (offset) <50) {this-> setContentOffsetInDuration (vec, 0.1f); 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-> 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: Forward (offset, dt ); this-> schedule (Alert (XKPageView: Alert);} void XKPageView: Your medanimatedscroll (float dt) {if (_ dragging) {this-> unschedule (Alert (XKPageView:: performedAnimatedScroll); 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 ;}

HelloWorld. h for testing


#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__  #include "cocos2d.h"#include "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);    Layer *getContainer();    // PageViewDelegate    virtual Size sizeForPerPage();    virtual void pageViewDidScroll(XKPageView *pageView);private:    XKPageView *pageView;    void addPages();};#endif // __HELLOWORLD_SCENE_H__

HelloWorld. cpp


# Include "HelloWorldScene. h "USING_NS_CC; # define COIN_WIDTH 212 # define COIN_GAP 30 # define COIN_COUNT 11Scene * HelloWorld: createScene () {// 'Scene 'is an autorelease object auto scene = scene :: create (); // 'player' is an autorelease object auto layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} // on "init" you need to initialize y Our instancebool HelloWorld: init () {// 1. super init first if (! Layer: init () {return false;} Size visibleSize = Director: getInstance ()-> getVisibleSize (); // XKPageView: create (visible range, XKPageViewDelegate, container); // auto page = XKPageView: create (Size (visibleSize. width, COIN_WIDTH), this, this-> getContainer (); pageView = XKPageView: create (Size (visibleSize. width, COIN_WIDTH), this); pageView-> setDirection (ScrollView: Direction: HORIZONTAL); // XKPage Scroll area of the View // page-> setContentSize (Size (COIN_WIDTH + COIN_GAP) * COIN_COUNT, COIN_WIDTH )); // container locates in the center of the screen pageView-> setPosition (Point (visibleSize. width-COIN_WIDTH) * 0.5, (visibleSize. height-COIN_WIDTH) * 0.5); addPages (); // You can crop the value to false to display the Size of pageView overflow by layer ~ PageView-> setClippingToBounds (false); this-> addChild (pageView);/* test function only */Node * node = pageView-> getPageAtIndex (5 ); log ("tag = % d", node-> getTag (); node-> setScale (1.3f); return true;} void HelloWorld: addPages () {Size coinSize = Sprite: create ("coin.png")-> getContentSize (); // Add 11 layers to the layer for (int I = 0; I <COIN_COUNT; I ++) {auto sprite = Sprite: create ("coin.png"); sprite-> s EtPosition (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); pageView-> addPage (sprite);} Layer * HelloWorld: getContainer () {Auto layer = Layer: create (); Size coinSize = Sprite: create ("coin.png")-> getContentSize (); // Add 11 sprite instances to _ iner for (int I = 0; I <COIN_COUNT; I ++) {auto sprite = Sprite: create ("coin.png "); sprite-> setPosition (coinSize. width * 0.5 + I * (coinSize. width + COIN_GAP), coinSize. height * 0.5); layer-> addChild (sprite); std: string str = StringUtils: format ("% d", I); Label * label = Lab El: createWithSystemFont (str, "Arial", 60); label-> setTextColor (Color4B (0,0, 0,255); Size size Size = sprite-> getContentSize (); label-> setPosition (size. width * 0.5, size. height * 0.5); sprite-> addChild (label);} return layer;} Size HelloWorld: sizeForPerPage () {// Delegate, returns the Size return Size (COIN_WIDTH + COIN_GAP, COIN_WIDTH) of each Page;} void HelloWorld: pageViewDidScroll (XKPageView * pageView ){ // Listen to the scroll time. You can write the code to be added when scrolling, such as scaling ~ Log ("pageViewDidScroll ");}

I have put all the code and images on CSDN. You can download them if you need them ~

Http://download.csdn.net/detail/joueu/8202549

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.