Cocos2d-x3.2下实现 可联动的表格

效果图:

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

使用方法:

 setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);

        auto winSize = Director::getInstance()->getWinSize();

        auto tableFormLayout = TableFormLayout::create();
        tableFormLayout->setSize(Size(winSize.width, winSize.height - 250 * VisibleRect::getImageScaleY()));

        tableFormLayout->addTableView(3);
        addChild(tableFormLayout);

        auto rp_sum_layout = ui::RelativeLayoutParameter::create();
        rp_sum_layout->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM);
        tableFormLayout->setLayoutParameter(rp_sum_layout);

下面粘贴具体的实现:之后我会将代码传上来

先粘贴下一个工具类这个工具在传代码时没有一起传上去。

VisibleRect.h

#ifndef __VISIBLERECT_H__
#define __VISIBLERECT_H__

#include "cocos2d.h"
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<unistd.h>
#include<sys/stat.h>
#include<string.h>

#define Blue_Color        Color4B(241, 104, 60, 255)//实际是橙色               之前上年累计
#define Green_Color       Color4B(42, 214, 42, 255) //实际是绿色        前年累计
#define Red_Color          Color4B(29, 139, 209, 255)//实际是蓝色        本年累计
#define purple_weak4            Color4B(93, 114, 123,255)  //柔和紫色

#define Blue_Color1     Color4B(42, 214, 42, 255)  //实际是绿色                之前上年累计
#define Green_Color1   Color4B(241, 104, 60, 255)//实际是橙色           前年累计

//#define Red_Color       Color4B(243, 152, 0, 255)
//#define Green_Color     Color4B(143, 195, 31, 255)
//#define Blue_Color      Color4B(0, 160, 233, 255) 

#define Blue_weak  Color3B(89, 203, 222)  //柔和蓝色
#define purple_weak      Color4B(189, 203, 222,255)  //柔和紫色

#define City_Color      Color4B(39, 73, 150, 255)

#define HTTP_URLTest "http://10.1.1.99/jsp/mobileData/"
#define HTTP_URL     "http://10.1.1.121:7080/MobileData/MobileData/getData?"

#define HTTP_Header "http://10.1.1.121:7080/MobileData/MobileData/"

#include "cocos2d.h"

USING_NS_CC;

class VisibleRect  :public Ref
{
public:
    static cocos2d::Rect getVisibleRect();

    static cocos2d::Vec2 left();
    static cocos2d::Vec2 right();
    static cocos2d::Vec2 top();
    static cocos2d::Vec2 bottom();
    static cocos2d::Vec2 center();
    static cocos2d::Vec2 leftTop();
    static cocos2d::Vec2 rightTop();
    static cocos2d::Vec2 leftBottom();
    static cocos2d::Vec2 rightBottom();

    static float getTitleFont();
    static float getLabelFont();

    static std::string getcurrTime();

    static std::string getcurrMonthTime();

    static std::string getTable_update_time(std::string tablename);

    static void deleteFile(std::string filename);

    static std::vector<std::string> getFilePathAtVec(std::string filePath);

    static float getImageScale();

    static float getImageScaleY();

    static std::vector<std::string> getAuthVec();

    static bool getIsVersion();

    static bool IsVersionDownload();

    static void updateApk();

private:
    static void lazyInit();
    static cocos2d::Rect s_visibleRect;
};

#endif /* __VISIBLERECT_H__ */

VisibleRect.cpp

#include "VisibleRect.h"
#include "external/json/document.h"
#include <stdio.h>

USING_NS_CC;

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "../../cocos2d/cocos/platform/android/jni/JniHelper.h"
#include <android/log.h>

#if 1
#define  LOG_TAG    "JniHelper"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define  LOGD(...)
#endif
#endif

Rect VisibleRect::s_visibleRect;

void VisibleRect::lazyInit()
{
    // no lazy init
    // Useful if we change the resolution in runtime
    s_visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect();
}

Rect VisibleRect::getVisibleRect()
{
    lazyInit();
    return s_visibleRect;
}

Vec2 VisibleRect::left()
{
    lazyInit();
    return Vec2(s_visibleRect.origin.x, s_visibleRect.origin.y+s_visibleRect.size.height/2);
}

Vec2 VisibleRect::right()
{
    lazyInit();
    return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width, s_visibleRect.origin.y+s_visibleRect.size.height/2);
}

Vec2 VisibleRect::top()
{
    lazyInit();
    return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width/2, s_visibleRect.origin.y+s_visibleRect.size.height);
}

Vec2 VisibleRect::bottom()
{
    lazyInit();
    return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width/2, s_visibleRect.origin.y);
}

Vec2 VisibleRect::center()
{
    lazyInit();
    return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width/2, s_visibleRect.origin.y+s_visibleRect.size.height/2);
}

Vec2 VisibleRect::leftTop()
{
    lazyInit();
    return Vec2(s_visibleRect.origin.x, s_visibleRect.origin.y+s_visibleRect.size.height);
}

Vec2 VisibleRect::rightTop()
{
    lazyInit();
    return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width, s_visibleRect.origin.y+s_visibleRect.size.height);
}

Vec2 VisibleRect::leftBottom()
{
    lazyInit();
    return s_visibleRect.origin;
}

Vec2 VisibleRect::rightBottom()
{
    lazyInit();
    return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width, s_visibleRect.origin.y);
}

std::string VisibleRect::getcurrTime()
{

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    struct timeval now;
    struct tm* time;

    gettimeofday(&now, NULL);

    time = localtime(&now.tv_sec);
    int year = time->tm_year + 1900;
    log("year = %d",year);

    char date[32] = {0};
    sprintf(date, "%d-%02d-%02d",(int)time->tm_year + 1900,(int)time->tm_mon + 1,(int)time->tm_mday);
    log("%s",date);
    return StringUtils::format("%s",date);

    #endif

    #if ( CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )

    struct tm* tm;
    time_t timep;
    time(timep);

    tm = localtime(&timep);
    char date[32] = {0};
    sprintf(date, "%d-%02d-%02d",(int)time->tm_year + 1900,(int)time->tm_mon + 1,(int)time->tm_mday);
    log("%s",date);
     return StringUtils::format("%s",date);

    #endif

}

std::string VisibleRect::getcurrMonthTime()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    struct timeval now;
    struct tm* time;

    gettimeofday(&now, NULL);

    time = localtime(&now.tv_sec);
    int year = time->tm_year + 1900;
    log("year = %d",year);

    char date[32] = {0};
    sprintf(date, "%d%02d%02d%02d%02d%02d",(int)time->tm_year + 1900,(int)time->tm_mon + 1,(int)time->tm_mday,time->tm_hour,time->tm_min,time->tm_sec);
    log("%s",date);
    return StringUtils::format("%s",date);

#endif

#if ( CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )

    struct tm* tm;
    time_t timep;
    time(timep);

    tm = localtime(&timep);
    char date[32] = {0};
    sprintf(date, "%d-%02d-%02d",(int)time->tm_year + 1900,(int)time->tm_mon + 1,(int)time->tm_mday);
    log("%s",date);
    return StringUtils::format("%s",date);

#endif

}

float VisibleRect::getTitleFont()
{
    auto winSize = Director::getInstance()->getWinSize();

    float fontSize_title = 35;
    if (winSize.height <= 960.0f) {
        fontSize_title = 25;
    }else if (winSize.height < 1140.0f && winSize.height > 960.0f)
    {
        fontSize_title = 30;
    }else{
        fontSize_title = 40;
    }

    return fontSize_title;

}

float VisibleRect::getLabelFont()
{

    auto winSize = Director::getInstance()->getWinSize();

    float fontSize_title = 25;
    if (winSize.height < 1000.0f) {
        fontSize_title = 20;
    }else if (winSize.height < 1140.0f && winSize.height >= 1000.0f)
    {
        fontSize_title = 25;
    }else{
        fontSize_title = 30;
    }

    return fontSize_title;

}

std::string VisibleRect::getTable_update_time(std::string tablename)
{
    std::string file_path_update = FileUtils::getInstance()->getWritablePath() + "update.json";

    if (!FileUtils::getInstance()->isFileExist(file_path_update))
    {
        file_path_update = FileUtils::getInstance()->fullPathForFilename("update.json");
    }

    rapidjson::Document d1 ;
    std::string contentStr = FileUtils::getInstance()->getStringFromFile(file_path_update);

    d1.Parse<0>(contentStr.c_str());

    const rapidjson::Value& v_time = d1["update_time"];
    unsigned int num = 0;
    const rapidjson::Value& v_time_vec = v_time[num];

    const rapidjson::Value& v_time_table = v_time_vec[tablename.c_str()];
    if (v_time_table.IsString())
    {
        return v_time_table.GetString();
    }

    return "null";

}

void VisibleRect::deleteFile(std::string filename)
{
    std::string file_path_update = FileUtils::getInstance()->getWritablePath() + filename;

    int res = remove(file_path_update.c_str());
    if (!res) {
        log("%s  删除文件成功",file_path_update.c_str());
    }else{

         log("%s  删除文件出错",file_path_update.c_str());
    }

}

//获取目录下文件夹内容
std::vector<std::string> VisibleRect::getFilePathAtVec(std::string filePath)
{
    std::vector<std::string> path_vec;
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
    int i=0;

    if((dp=opendir(filePath.c_str()))==NULL)
    {
        fprintf(stderr,"cannot open %s",filePath.c_str());
        exit(1);
    }
    chdir(filePath.c_str());

    while((entry=readdir(dp))!=NULL&&i<255)
    {
        stat(entry->d_name,&statbuf);
        if(!S_ISREG(statbuf.st_mode))
            continue;
        path_vec.push_back(StringUtils::format("%s",entry->d_name));
    }
    return path_vec;
}

float VisibleRect::getImageScale()
{
    lazyInit();
    log("%f",s_visibleRect.size.width);
    return s_visibleRect.size.width / 1080;
}

float VisibleRect::getImageScaleY()
{
    lazyInit();
    return s_visibleRect.size.height / 1920;
}

std::vector<std::string> VisibleRect::getAuthVec()
{
   auto path_title = FileUtils::getInstance()->getWritablePath() +  "role.json" ;
   auto contentStr = FileUtils::getInstance()->getStringFromFile(path_title);
     rapidjson::Document d1;

    std::vector<std::string>  role_name;
    role_name.push_back("f_mobile_kpi_day");
    role_name.push_back("f_mobile_car_mqpfl_org");
    role_name.push_back("f_mobile_nocar");
    role_name.push_back("f_mobile_per");
    role_name.push_back("f_mobile_ea");
    role_name.push_back("f_mobile_qd");

    std::vector<std::string>  auth_vec;

    d1.Parse<0>(contentStr.c_str());
    if (d1.IsObject())
    {
        for (int j = 0; j < role_name.size(); j++)
        {
            const rapidjson::Value &val1 = d1[role_name.at(j).c_str()];

            auth_vec.push_back(val1.GetString());
        }

    }
    return auth_vec;
}
#pragma mark - 获取版本是否需要更新  为真需要更新
bool VisibleRect::getIsVersion()
{

    auto path_title = FileUtils::getInstance()->getWritablePath() +  "version.json";
    auto contentStr = FileUtils::getInstance()->getStringFromFile(path_title);
    rapidjson::Document d1;
    d1.Parse<0>(contentStr.c_str());
    auto phone_version = UserDefault::getInstance()->getStringForKey("version");

    if (strcmp(d1["version"].GetString(), "1.0") != 0&& strcmp(d1["version"].GetString(), phone_version.c_str()) != 0 )
    {
        return true;
    }
    return false;

}

#pragma mark - 获取版本是否需要更新  为真需要更新
bool VisibleRect::IsVersionDownload()
{

    auto phone_version = UserDefault::getInstance()->getStringForKey("current_version");

    auto download_version = UserDefault::getInstance()->getStringForKey("download_version");

    if (strcmp(download_version.c_str(), phone_version.c_str()) != 0 )
    {
        return true;
    }
    return false;

}

void VisibleRect::updateApk()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    log("进入更新");
    JniMethodInfo minfo;
    jobject jobj;
    bool b = JniHelper::getStaticMethodInfo(minfo,
                                            "org/cocos2dx/cpp/AppActivity",  //类路径
                                            "cppCall_logsth",   //静态方法名
                                            "(Ljava/lang/String;)Ljava/lang/Object;");   //括号里的是参数,后面的是返回值。

    if (!b) {
        LOGD("JniHelper::getMethodInfo error...");
    }else
    {
        std::string key = FileUtils::getInstance()->getWritablePath() + "update_mobile.apk";
        jstring jkey = minfo.env->NewStringUTF(key.c_str());
        jobj =   minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,jkey);

    }

    b = JniHelper::getMethodInfo(minfo, "org/cocos2dx/cpp/AppActivity",  //类路径
                                 "updateversion",   //静态方法名
                                 "(Ljava/lang/String;)V");   //括号里的是参数,后面的是返回值。
    if (!b) {
        LOGD("JniHelper::getMethodInfo error...");
    }else
    {
        std::string key = FileUtils::getInstance()->getWritablePath() + "update_mobile.apk";
//        std::string key = FileUtils::getInstance()->fullPathForFilename("update_mobile.apk");
        jstring jkey = minfo.env->NewStringUTF(key.c_str());
        minfo.env->CallVoidMethod(jobj, minfo.methodID,jkey);

    }

#endif

}

TableFormView.h

//
//  TableFormView.h
//
//
//  Created by 杜甲 on 14-7-31.
//
//

#ifndef ____TableFormView__
#define ____TableFormView__

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

USING_NS_CC;

class TableFormView:public Layer
{

public:
    CREATE_FUNC(TableFormView);

    virtual bool init();

    CC_SYNTHESIZE(int, labelWidth, LabelWidth);
    CC_SYNTHESIZE(int, labelHeight, LabelHeight);

    void initWithSection(int sectionNum,int rowNum,int fontSize);

    void initWithData(std::vector<std::string> inData);
    int i_addBorderColorFlag; //表格底部外框颜色标志,给表格中label添加外框(分割线)
    int i_fontFlag;   //设置大的字体,第一列设置为大的字体
    int i_colorFlag;  //颜色标志
    int i_productFlag; //产品线专项显示标志

    Size getTableFormSize();

private:

    int i_beforerow;   //记录是否换行
    Vector<Label*> vec_Text;

    float getImageScale();
    float getImageScaleY();

    Size tableFormSize;

};

#endif /* defined(__picc_mobile__TableFormView__) */

TableFormView.cpp

//
//  TableFormView.cpp
//
//
//  Created by 杜甲 on 14-7-31.
//
//

#include "TableFormView.h"
bool TableFormView::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!Layer::init());

//        auto sprite = Sprite::create("HelloWorld.png");
//
//
//        addChild(sprite);

//        setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);

        bRet = true;
    } while (0);
    return bRet;
}

void TableFormView::initWithSection(int sectionNum, int rowNum,int fontSize)
{
    if (labelWidth == 0) {
        labelWidth = 80 * getImageScale();
    }

    if (labelHeight == 0) {
        labelHeight = 35 * getImageScaleY();
    }

    for (int j = 0; j < rowNum; j++)
    {
        for (int i = 0; i < sectionNum; i++)
        {
            Color4B layer_color;

            if (i_colorFlag == 1) //表头着色
            {
                if (j == 0)
                {
                    layer_color = Color4B(54, 107, 12, 255);
                }else
                {
                    layer_color = Color4B(237, 237, 237, 255);
                }
            }else if (i_colorFlag == 2)
            {
                layer_color = Color4B(100, 160, 52, 255);
            }else{
                if (j % 2 == 0)
                {
                    layer_color = Color4B(155, 210, 111, 255);
                }else{
                    layer_color = Color4B(214, 245, 188, 255);
                }
            }

            auto label_layer = LayerColor::create(layer_color, labelWidth, labelHeight);
            label_layer->setPosition(Vec2(i  * labelWidth, (rowNum -j - 1) * labelHeight));
            addChild(label_layer);

            auto label = Label::create();
            vec_Text.pushBack(label);
            label->setColor(Color3B::BLACK);
            label->setString(StringUtils::format("%d%d",j,i));
            label->setSystemFontSize(fontSize);
            label->setPosition(Vec2(label_layer->getContentSize().width / 2, label_layer->getContentSize().height / 2));
            label->setAlignment(cocos2d::TextHAlignment::CENTER);
            label_layer->addChild(label);

        }

    }

    tableFormSize = Size(labelWidth * sectionNum, labelHeight * rowNum);

}

void TableFormView::initWithData(std::vector<std::string> inData)
{

}

Size TableFormView::getTableFormSize()
{
    return tableFormSize;
}

float TableFormView::getImageScale()
{

    return Director::getInstance()->getWinSize().width / 1080;
}

float TableFormView::getImageScaleY()
{
    return Director::getInstance()->getWinSize().height / 1920;
}

TableFormScrollView.h

//
//  TableFormScrollView.h
//
//
//  Created by 杜甲 on 14-7-31.
//
//

#ifndef __TableFormScrollView__
#define __TableFormScrollView__

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

#include "TableFormView.h"

USING_NS_CC;
typedef enum
{
    Header_RightTag = 100,
    City_Tag        = 101,
    City_Name       = 102
}ScrollViewTag;

class TableFormScrollView;
class TableFormScrollViewDelegate {

public:
    virtual void scrollViewToEdge() = 0;
    virtual void scrollViewScrolling() = 0;

};

class TableFormScrollView : public ui::Layout ,public extension::ScrollViewDelegate
{
public:

    CREATE_FUNC(TableFormScrollView);

    virtual bool init();

    CC_SYNTHESIZE(TableFormScrollViewDelegate*, _delegate, Delegate);

    CC_SYNTHESIZE_RETAIN(extension::ScrollView*, _headerLeftScrollView, HeaderLeftScrollView);
    CC_SYNTHESIZE_RETAIN(extension::ScrollView*, _cityScrollView1, CityScrollView1);

    CC_SYNTHESIZE_RETAIN(extension::ScrollView*, _headerRightScrollView2, HeaderRightScrollView2);
    CC_SYNTHESIZE_RETAIN(extension::ScrollView*, _cityNameScrollView3, CityNameScrollView3);

    CC_SYNTHESIZE_RETAIN(TableFormView*, _ibCityTableView, ibCityTableView);
    CC_SYNTHESIZE_RETAIN(TableFormView*, _ibCityNameTableView, ibCityNameTableView);
    CC_SYNTHESIZE_RETAIN(TableFormView*, _ibCityHeaderRightTableView, ibCityHeaderRightTableView);
    CC_SYNTHESIZE_RETAIN(TableFormView*, _ibCityHeaderLeftTableView, ibCityHeaderLeftTableView);

    void setHeaderLeftAttribute(int fontFlag, int colorFlag, int section, int row ,float textWidth,float textHeight);
    void setHeaderRightAttribute(int fontFlag,int colorFlag , int section , int row,float textWidth,float textHeight);

    void setCityAttribute(int fontFlag,int colorFlag , int section , int row,float textWidth,float textHeight);
    void setCityNameAttribute(int fontFlag,int colorFlag , int section , int row,float textWidth,float textHeight);

    /*获取滚动视图是否到边界,true为到边界,false为没有*/
    bool getEdge();

private:
    Size winSize;
    virtual void scrollViewDidScroll(extension::ScrollView* view);
    /**
     * @js NA
     * @lua NA
     */
    virtual void scrollViewDidZoom(extension::ScrollView* view);
     bool isEdge;

};

#endif /* defined(__picc_mobile__TableFormScrollView__) */

TableFormLayout.h

//
//  TableFormLayout.h
//
//
//  Created by 杜甲 on 14-8-3.
//
//

#ifndef __TableFormLayout__
#define __TableFormLayout__

#include "cocos2d.h"
#include "ui/cocosGUI.h"
#include "TableFormScrollView.h"

USING_NS_CC;

class TableFormLayout: public ui::Layout , public TableFormScrollViewDelegate
{
public:

    CREATE_FUNC(TableFormLayout);

    virtual bool init();

    float click_num;

    double beforeTouchTime; //上次触摸时间

    virtual void scrollViewToEdge();
    virtual void scrollViewScrolling();

    void addTableView(int num);

private:
    bool isEdge;

    Vec2 beforePoint;

    Vec2 firstPoint;

    int index;

};

#endif /* defined(__picc_mobile__TableFormLayout__) */

TableFormLayout.cpp

//
//  TableFormLayout.cpp
//
//
//  Created by 杜甲 on 14-8-3.
//
//

#include "TableFormLayout.h"

#include "../Tools/widget/VisibleRect.h"

bool TableFormLayout::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!ui::Layout::init());

//        setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);

        index = 0;

        bRet = true;
    } while (0);
    return bRet;
}

void TableFormLayout::addTableView(int num)
{

    auto sum_layout = ui::Layout::create();
    sum_layout->setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);
    sum_layout->setSize(Size(getSize().width * num, getSize().height));
//    sum_layout->setPosition(Vec2(0, getSize().height / 2));
    addChild(sum_layout);

//    auto rp_sum_layout = ui::RelativeLayoutParameter::create();
//    rp_sum_layout->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT);
//    sum_layout->setLayoutParameter(rp_sum_layout);

    for (int i = 0; i < num; i++)
    {
        auto tableForm = TableFormScrollView::create();

        tableForm->setPosition(Vec2(getSize().width * i, 0));
        if (i != 0) {
            tableForm->setPosition(Vec2(getSize().width , 0));
        }
        tableForm->setDelegate(this);
        tableForm->setSize(getSize());
        tableForm->setHeaderLeftAttribute(1, 1, 2, 2, 90, 90);

        tableForm->setHeaderRightAttribute(1, 1, 19, 2, 90, 90);
        tableForm->setCityNameAttribute(1, 1, 2, 7, 90, 90);
        tableForm->setCityAttribute(1, 1, 19, 7, 90, 90);
        sum_layout->addChild(tableForm);

        auto rp_tableForm = ui::RelativeLayoutParameter::create();
        if (i == 0)
        {
            rp_tableForm->setRelativeName(StringUtils::format("tableForm%d",i));
             rp_tableForm->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM);
        }else{
            rp_tableForm->setRelativeName(StringUtils::format("tableForm%d",i));
            rp_tableForm->setRelativeToWidgetName(StringUtils::format("tableForm%d",i - 1));
            rp_tableForm->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER);
        }
        tableForm->setLayoutParameter(rp_tableForm);

    }

    auto sprite = Sprite::create("HelloWorld.png");
    addChild(sprite);
    isEdge = false;
    beforeTouchTime = 0;

    Size layout_size = getSize();

    log("%f",layout_size.width);

    auto listener1 = EventListenerTouchOneByOne::create();
    listener1->setSwallowTouches(true);
    listener1->onTouchBegan = [=](Touch* touch, Event* event)
    {
        log("began");
        auto target = event->getCurrentTarget();
        Point locationInNode = target->convertToNodeSpace(touch->getLocation());
        log("%f,%f",touch->getLocation().x,touch->getLocation().y);
        beforePoint = touch->getLocation();
        firstPoint = touch->getLocation();

        //            if ((atof(VisibleRect::getcurrMonthTime().c_str()) - beforeTouchTime < 1 ))
        //            {
        //
        //                tableForm->runAction(MoveTo::create(1.0f, Vec2(-Director::getInstance()->getWinSize().width, tableForm->getPosition().y)));
        //
        //
        //            }
        beforeTouchTime = atof(VisibleRect::getcurrMonthTime().c_str());
        if (isEdge) {
            return true;
        }else{
            return false;
        }

    };

    listener1->onTouchMoved = [=](Touch* touch, Event* event)
    {
        float offset = beforePoint.x - touch->getLocation().x ;

        sum_layout->setPosition(Vec2(sum_layout->getPosition().x - offset, sum_layout->getPosition().y));
        beforePoint = touch->getLocation();

    };

    listener1->onTouchEnded = [ = ](Touch* touch, Event* event)
    {
        log("%f",layout_size.width);
        if (firstPoint.x - beforePoint.x > getSize().width/3)
        {
            index += (num + 1) % num;
            if (index >= num - 1) {
                index = num - 1;
            }
            sum_layout->runAction(MoveTo::create(0.5f, Vec2(-layout_size.width * index,  sum_layout->getPosition().y)));

        }else if (firstPoint.x - beforePoint.x < -getSize().width/3)
        {
            index -= (num - 1) % num;
            if (index <= 0)
            {
                index = 0;
            }
            sum_layout->runAction(MoveTo::create(0.5f, Vec2(-layout_size.width * index,  sum_layout->getPosition().y)));
        }else
        {
            sum_layout->runAction(MoveTo::create(0.5f, Vec2(-layout_size.width * index,  sum_layout->getPosition().y)));
        }

        isEdge = false;
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite);

}

void TableFormLayout::scrollViewToEdge()
{
    isEdge = true;
}

void TableFormLayout::scrollViewScrolling()
{
    isEdge = false;
}

Cocos2d-x3.2下实现 可联动的表格,布布扣,bubuko.com

时间: 2024-10-07 21:37:48

Cocos2d-x3.2下实现 可联动的表格的相关文章

ajax技术实现登录判断用户名是否重复以及利用xml实现二级下拉框联动

今天学了ajax技术,特地在此写下来作为复习. 一.什么是ajax? 客户端(特指PC浏览器)与服务器,可以在[不必刷新整个浏览器]的情况下,与服务器进行异步通讯的技术  即,AJAX是一个[局部刷新]的[异步]通讯技术, 说白了就是局部刷新. 二.ajax的原理如下图 附上ajax与服务器之间的几种状态,但 4是所有浏览器都支持的的 三.ajax包含的技术如下图 四.ajax开发步骤 步一:创建ajax对象,例如:ajax = createAjax(); 步二:开启异步对象:例如:ajax.o

JQuery打造下拉框联动效果

做联动效果,若是用纯JavaScript来做,往往须要辅助页面保存须要刷新的结果集,然后渲染到原页面.考虑将须要动态刷新的内容自己主动拼接到前一个下拉框之后,当前一个下拉框onchange后,同级的后面的下拉框所有清除,然后又一次拼接刷新的内容.用JQuery实现比較easy,代码以省市联动效果为例实现. JSP页面代码例如以下: <li id="base"> <p>生源地:</p> <label> <select id="

基于bootstrap-multiselect.js的下拉框联动

背景:当option特别多时,一般的下拉框选择起来就有点力不从心了,所以使用multiselect是个很好的选择,可以通过输入文字来选择选项很方便,但是有一个需要下拉框联动,网上找了半天才找到解决方法,在此分享一下 1.先引入 <script src="~/Assets/js/bootstrap-multiselect.min.js"></script> <link href="~/Assets/css/bootstrap-multiselect

java 下拉多级联动 头像裁剪 调用摄像头 SpringMVC mybatis SSM

获取[下载地址]   QQ 313596790三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体[新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技术:313596790freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类,service等完整模块C 集成阿里

月薪10K必备--C#下拉框联动

               下拉框联动 很多网站上都用到下拉框联动,就是第一个下拉框没有选择任何项,第二个下拉框就没有选项.这样的做法更加谨慎,更加紧密. 下面我就教大家怎么做下拉框联动: 首先在窗体的Load事件中给第一个下拉框赋选项 然后第一个下拉框就有选项了 然后我们在第一个下拉框的SelectedIndexChanged()事件中(也就是双击下拉框)写第二个下拉框的代码 这样的话,只要第一个下拉框没选中一项,第二个下拉框就不会有选择项! 下拉框联动就是这样,希望对读者多多少少有点帮助!

Java Swing应用程序 JComboBox下拉框联动查询

在web项目中,通过下拉框.JQuery和ajax可以实现下拉框联动查询. 譬如说,当你查询某个地方时,页面上有:省份:<下拉框省份> 市区:<下拉框市区> 县乡:<下拉框县乡> 街道:<街道下拉框> 查询 譬如说,你选择了省:江苏省,那么在市区中只会显示江苏省的市区 譬如:(网上的图) 具体详细实现可以参考,写得挺好的:http://blog.csdn.net/sinat_24491773/article/details/50810471 那么在swing

html年月日下拉联动菜单 年月日三下拉框联动

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head><title>年月日三下拉框联动</title>

程矢Axure夜话:中继器系列视频教程之中继器下拉菜单联动

程矢Axure夜话:中继器系列视频教程之中继器下拉菜单联动 案例描述:中继器下拉菜单联动 知识点: 中继器复合操作 效果图: 在线效果预览: AxShare在线效果预览: 原型下载地址:中继器拉菜单联动 .rp 在线视频: 实现步骤: 更新中-..

下拉框联动方法封装

业务中经常需要对数据进行下拉框的联动选择操作,可以假设成省份城市 省份城市县这样的多级联动 客户那边提供的数据大多为excel,格式都属于标准一行列的 假设需要对省份城市进行联动 实现如下 1 var pcd = []; 2 pcd[0] = ['北京', '北京']; 3 pcd[1] = ['天津', '天津']; 4 pcd[2] = ['河北', '石家庄']; 5 pcd[3] = ['河北', '唐山']; 6 pcd[4] = ['山西', '太原']; 7 pcd[5] = ['