首页 > 代码库 > Cocos2d-x3.0下实现循环列表

Cocos2d-x3.0下实现循环列表

本文的实现是參照我之前在做iOS时实现的一个能够循环的列表这里用C++重写一遍。

效果:



原文地址:http://blog.csdn.net/qqmcy/article/details/27393011

用法:能够使用创建project时的HelloWorld測试

,h文件里加入循环列表的监听回调

 void djInfiniteScroll(Ref* sender);
    


.cpp

#include "DJInfiniteScrollPicker.h"

        DJInfiniteScrollPicker* picker = DJInfiniteScrollPicker::create();
        picker->setSize(Size(500, 500));
        picker->imageAry = set1;
        picker->setPosition(Point(10, 100));
        picker->setBackGroundColorType(cocos2d::ui::LAYOUT_COLOR_SOLID);
        picker->setBackGroundColor(Color3B(128, 128, 128));
        picker->initInfiniteScrollView();
        
        addChild(picker);
        
        picker->addEventDJInifiteScrollPicker(this, djInfinitescrollvieweventselector(TestScene::djInfiniteScroll));

实现类:

DJInfiniteScrollPicker.h

//
//  DJInfiniteScrollPicker.h
//  maptest
//
//  Created by 杜甲 on 14-5-27.
//
//

#ifndef __maptest__DJInfiniteScrollPicker__
#define __maptest__DJInfiniteScrollPicker__

#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "extensions/cocos-ext.h"

USING_NS_CC;
typedef enum
{
    sc = 0,
    sc1
}ScType;

typedef void (Ref::*SEL_InfiniteScrollViewEvent)(Ref*);

#define djInfinitescrollvieweventselector(_SELECTOR) (SEL_InfiniteScrollViewEvent)(&_SELECTOR)


class DJInfiniteScrollPicker:public  ui::Layout,public extension::ScrollViewDelegate
{
    
public:
    CREATE_FUNC(DJInfiniteScrollPicker);
    virtual bool init();
    
    static DJInfiniteScrollPicker* create(Size wsize);
    virtual bool initWithSize(Size wsize);
    


    virtual void scrollViewDidScroll(extension::ScrollView* view);
    /**
     * @js NA
     * @lua NA
     */
    virtual void scrollViewDidZoom(extension::ScrollView* view);
    
    void initInfiniteScrollView();
    

    extension::ScrollView* infiniteScrollView;
    
    std::vector<std::string> imageAry;
    Size itemSize;
    float alphaOfobjs;
    
    float heightOffset;
    float positionRatio;
    
    void addEventDJInifiteScrollPicker(Ref* sender,SEL_InfiniteScrollViewEvent type);
    
    
    
    
private:
    std::vector<Sprite*> imageStore;
    

    float beforePointX;
    std::string fontName;
    void reloadView(float offset);
    void snapToAnEmotion();
    
    Sprite* currSprite;
    bool isFirstDrag;
    int currNum;
    
    Ref* _infiniteEventListener;
    SEL_InfiniteScrollViewEvent _infiniteEventSelector;
    
    
    

};

#endif /* defined(__maptest__DJInfiniteScrollPicker__) */

DJInfiniteScrollPicker.cpp

//
//  DJInfiniteScrollPicker.cpp
//  maptest
//
//  Created by 杜甲 on 14-5-27.
//
//

#include "DJInfiniteScrollPicker.h"
bool DJInfiniteScrollPicker::init()
{
    bool bRet = false;

    do {
        CC_BREAK_IF(!ui::Layout::init());
        
        bRet = true;
    } while (0);
    return bRet;
}

DJInfiniteScrollPicker* DJInfiniteScrollPicker::create(Size size)
{
    DJInfiniteScrollPicker* pRet = new DJInfiniteScrollPicker();
    if (pRet && pRet->initWithSize(size))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    return pRet;

}

bool DJInfiniteScrollPicker::initWithSize(cocos2d::Size size)
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!ui::Layout::init());
        
        
      
        
        
        
        bRet = true;
    } while (0);
    return bRet;
}




void DJInfiniteScrollPicker::initInfiniteScrollView()
{
    isFirstDrag = false;
    fontName = "TrebuchetMS-Bold";
    
    
    alphaOfobjs = 1.0f;
    heightOffset = 0.0f;
    positionRatio = 1.0f;
    
    beforePointX = 0.0f;
    
    infiniteScrollView = extension::ScrollView::create(getSize());
    infiniteScrollView->retain();
    infiniteScrollView->setPosition(Point(0, 0));
    
    
    infiniteScrollView->setBounceable(false);
    infiniteScrollView->setDelegate(this);
    //infiniteScrollView->setContentSize(get);
    addChild(infiniteScrollView);
    
   Sprite* temp = Sprite::create("res/set2/s2_1.png");
//    log("anchor = %f",temp->getAnchorPoint().x);
//    temp->setPosition(Point(temp->getContentSize().width / 2, temp->getContentSize().height / 2));
//    // temp->setPosition(Point( itemSize.width, 0));
//    infiniteScrollView->addChild(temp);
    
    
    if (imageAry.size() > 0) {
        
        itemSize  = temp->getContentSize();
    }
    
    
    
//    
    if (imageAry.size() > 0) {
        for (int i = 0; i < (imageAry.size() * 5); i++) {
            //ui::Text* temp = ui::Text::create(StringUtils::format("%d",i % (int)imageAry.size()), fontName, 23);
            Sprite* sprite = Sprite::create(imageAry.at(i % imageAry.size()));
            sprite->setTag(i % imageAry.size());
            sprite->setPosition(Point(i * itemSize.width + itemSize.width / 2, itemSize.height / 2));
            imageStore.push_back(sprite);
            infiniteScrollView->addChild(sprite);
        }
    }
   

    
    infiniteScrollView->setContentSize(Size(imageAry.size() * 5 * itemSize.width, getSize().height));
    float viewMiddle = imageAry.size() * 2 * itemSize.width;
    
    infiniteScrollView->setContentOffset(Point(-viewMiddle , 0));
    
    
}

void DJInfiniteScrollPicker::scrollViewDidScroll(extension::ScrollView *view)
{
    float sectionSize = imageAry.size() * itemSize.width;
    log("QQQQQQQQQQQQ%f,%f",infiniteScrollView->getContentOffset().x,beforePointX);
    
    
    
    if (beforePointX != infiniteScrollView->getContentOffset().x)
    {
        
        if (infiniteScrollView->getContentOffset().x < 0)
        {
            if (infiniteScrollView->getContentOffset().x >= (-sectionSize + sectionSize / 2))
            {
                view->setContentOffset(Point(-sectionSize * 2 +sectionSize / 2, 0));
            }
            else if (view->getContentOffset().x <= (-sectionSize * 3 - sectionSize / 2))
            {
                view->setContentOffset(Point(-sectionSize * 2 - sectionSize / 2, 0));
            }
           // if (infiniteScrollView->isDragging()) {
               // log("拖拽中");
                 reloadView(view->getContentOffset().x - getSize().width / 2);
         //   }
            
            
        }
        else
        {
            view->setContentOffset(Point(-sectionSize * 2 + sectionSize / 2, 0));
        }
        
        
        //推断是否被拖拽
        if (!infiniteScrollView->isDragging()) {
            
            //推断是否是首次进入,首次不拖拽
            if (isFirstDrag) {
                beforePointX = -currSprite->getPosition().x + getSize().width / 2;
                view->setContentOffset(Point(beforePointX, 0));
                  //log("停止拖拽view->getContentOffset().xQQ = %f",view->getContentOffset().x);
                if (_infiniteEventListener && _infiniteEventSelector) {
                    (_infiniteEventListener->*_infiniteEventSelector)(currSprite);
                    
                    
                }
                
//                if (_scrollViewEventListener && _scrollViewEventSelector)
//                {
//                    (_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_TOP);
//                }
                
            }
            
            
            isFirstDrag = true;
        }


    }
    log("view->getContentOffset().x = %f",view->getContentOffset().x);
    
    beforePointX = view->getContentOffset().x;
    
    
   
   
    
}

void DJInfiniteScrollPicker::reloadView(float offset)
{
   
    float biggestSize = 0.0f;
    Ref* biggestView;
    Sprite* sprite = imageStore.at(2);
    
    
    
    for (int i = 0; i < imageStore.size(); i++)
    {
        Sprite* sprite = imageStore.at(i);
        
        

        if (-sprite->getPosition().x > (offset - itemSize.width / 2) && -sprite->getPosition().x < (offset + itemSize.width / 2)) {

            
            currSprite = sprite;
            currNum = i;
            
            
            sprite->setScale(2);
            
        }else
        {
            sprite->setScale(1);
        }
    }
}



void DJInfiniteScrollPicker::snapToAnEmotion()
{
    
    float offset = infiniteScrollView->getContentOffset().x - getSize().width / 2;
    
    
    
}


void DJInfiniteScrollPicker::scrollViewDidZoom(extension::ScrollView *view)
{
    
}

void DJInfiniteScrollPicker::addEventDJInifiteScrollPicker(cocos2d::Ref *sender, SEL_InfiniteScrollViewEvent type)
{
    _infiniteEventListener = sender;
    _infiniteEventSelector = type;
}