.h

#ifndef __COMMON_COMPONENTS__
#define __COMMON_COMPONENTS__ #include "cocos2d.h"
#include "cocos-ext.h" USING_NS_CC;
USING_NS_CC_EXT; #define ROOT_BACK_WIDTH 380 //background width
#define ROOT_BACK_HEIGHT 450 //background height
#define LAYOUT_WIDTH 360 //each page width
#define LAYOUT_HEIGHT 360 //each page height
#define PAGE_SUM_NUM 6 //total page num
#define EACH_PAGE_NUM 9 //each page grid num class CCommonComponents : public cocos2d::extension::UILayer
{
public:
CCommonComponents(void);
~CCommonComponents(void); virtual bool init();
CREATE_FUNC(CCommonComponents); public:
UIImageView* createHorizontalGrid( int pageNum = PAGE_SUM_NUM );
void tagMenuTest( CCObject* pSender );
void refreshGrid();
void pageViewEvent(CCObject *pSender, PageViewEventType type);
void buttonTidyCallback( CCObject* pSender ); public:
UIImageView* m_pImgEffect[PAGE_SUM_NUM]; //effect of selected
UIButton* m_pBtnAllGrid[PAGE_SUM_NUM][EACH_PAGE_NUM]; //total num of grid
UIImageView* m_pBtnTextureChild[PAGE_SUM_NUM][EACH_PAGE_NUM]; //each icon in gird
UIImageView* m_pImgLight[PAGE_SUM_NUM]; //the current page where the lights
int m_IntCurPage; //record the current page number
}; #endif //__COMMON_COMPONENTS__

.cpp

#include "CommonComponents.h"

CCommonComponents::CCommonComponents(void) :
m_IntCurPage()
{
memset(m_pImgEffect, , sizeof(m_pImgEffect));
memset(m_pBtnAllGrid, , sizeof(m_pBtnAllGrid));
memset(m_pBtnTextureChild, , sizeof(m_pBtnTextureChild));
memset(m_pImgLight, , sizeof(m_pImgLight));
} CCommonComponents::~CCommonComponents(void)
{
} bool CCommonComponents::init()
{
bool bRet = false; do
{
CC_BREAK_IF( !UILayer::init() ); bRet = true; } while (); return bRet;
} UIImageView* CCommonComponents::createHorizontalGrid( int pageNum )
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize(); UIImageView* imgBack = UIImageView::create();
imgBack->setTexture("frame.png");
imgBack->setScale9Enable(true);
imgBack->setScale9Size(CCSizeMake(ROOT_BACK_WIDTH, ROOT_BACK_HEIGHT)); UIPageView* pageView = UIPageView::create();
pageView->setTouchEnable(true);
pageView->setSize(CCSizeMake(LAYOUT_WIDTH, LAYOUT_HEIGHT));
pageView->setAnchorPoint(ccp(0.5f, 0.5f)); for (int i = ; i < pageNum; ++i)
{
UIPanel* layout = UIPanel::create();
layout->setSize(CCSizeMake(LAYOUT_WIDTH, LAYOUT_HEIGHT)); UIImageView* imageView = UIImageView::create();
imageView->setTouchEnable(true);
imageView->setScale9Enable(true);
imageView->setTexture("pic_frame1.png");
imageView->setScale9Size(CCSizeMake(LAYOUT_WIDTH, LAYOUT_HEIGHT));
imageView->setPosition(ccp(layout->getRect().size.width / , layout->getRect().size.height / ));
layout->addChild(imageView); m_pImgEffect[i] = UIImageView::create();
m_pImgEffect[i]->setVisible(false);
m_pImgEffect[i]->setTexture("frame_pressed.png");
layout->addChild(m_pImgEffect[i]); m_pImgLight[i] = UIImageView::create();
if( == i )
{
m_pImgLight[i]->setTexture("green.png");
}
else
{
m_pImgLight[i]->setTexture("red.png");
}
m_pImgLight[i]->setPosition(ccp(- + i*, -)); for(int j=; j<; ++j)
{
m_pBtnAllGrid[i][j] = UIButton::create();
m_pBtnAllGrid[i][j]->setTouchEnable(true);
m_pBtnAllGrid[i][j]->setTextures("frame.png", "frame.png", "frame.png");
m_pBtnAllGrid[i][j]->setPosition(ccp( m_pBtnAllGrid[i][j]->getContentSize().width/+ j%*m_pBtnAllGrid[i][j]->getContentSize().width*1.2 + ,
layout->getSize().height-m_pBtnAllGrid[i][j]->getContentSize().height/-m_pBtnAllGrid[i][j]->getContentSize().height*(j/)/0.85 - ));
layout->addChild(m_pBtnAllGrid[i][j]);
m_pBtnAllGrid[i][j]->setWidgetTag(i*+j);
m_pBtnAllGrid[i][j]->addReleaseEvent(this, coco_releaseselector(CCommonComponents::tagMenuTest)); m_pBtnTextureChild[i][j] = UIImageView::create();
m_pBtnTextureChild[i][j]->setTexture("frame.png");
m_pBtnAllGrid[i][j]->addChild(m_pBtnTextureChild[i][j]);
} pageView->addPage(layout); imgBack->addChild(m_pImgLight[i]);
} pageView->addEventListener(this, pagevieweventselector(CCommonComponents::pageViewEvent));
imgBack->addChild(pageView);
pageView->setPosition(ccp(, )); UIButton* button = UIButton::create();
button->setTouchEnable(true);
button->setTextures("pic_button_1.png","pic_button_2.png","pic_button_3.png");
button->addReleaseEvent(this, coco_releaseselector(CCommonComponents::buttonTidyCallback));
imgBack->addChild(button);
button->setPosition(ccp(, -)); UILabel* labBtnFont = UILabel::create();
labBtnFont->setText("TIDY");
labBtnFont->setFontSize();
button->addChild(labBtnFont); refreshGrid(); //temporary test return imgBack;
} void CCommonComponents::tagMenuTest( CCObject* pSender )
{
UIButton* curImgClick = dynamic_cast<UIButton*>(pSender);
int curTag = curImgClick->getWidgetTag();
CCLOG("curTag = %d ; [%f, %f]", curTag, curImgClick->getPosition().x, curImgClick->getPosition().y); m_pImgEffect[m_IntCurPage]->setPosition(curImgClick->getPosition());
m_pImgEffect[m_IntCurPage]->setVisible(true); } void CCommonComponents::refreshGrid()
{
for(int i= ; i<; ++i)
{
for (int j = ; j<EACH_PAGE_NUM; ++j)
{
if( i*EACH_PAGE_NUM+j <= )
m_pBtnTextureChild[i][j]->setTexture("CloseNormal.png");
}
} } void CCommonComponents::pageViewEvent(CCObject *pSender, PageViewEventType type)
{
switch (type)
{
case PAGEVIEW_EVENT_TURNING:
{
UIPageView* pageView = dynamic_cast<UIPageView*>(pSender);
CCLog(" m_IntCurPage = %d",pageView->getPage());
if(m_IntCurPage != pageView->getPage())
{
m_IntCurPage = pageView->getPage();
for(int i=; i<PAGE_SUM_NUM; ++i)
{
m_pImgEffect[i]->setVisible(false);
if(i == m_IntCurPage)
{
m_pImgLight[i]->setTexture("green.png");
}
else
{
m_pImgLight[i]->setTexture("red.png");
}
}
}
}
break; default:
break;
}
} void CCommonComponents::buttonTidyCallback( CCObject* pSender )
{
CCLog("running buttonCallback function");
}

使用方式:

CCommonComponents* pageLayer =  CCommonComponents::create();
this->addChild(pageLayer);

显示效果:

   

这只是一个小的例子代码,实现方式是依赖 cocostudio 库函数,没有好,只有比较好。

欢迎来访并指教 QQ:316948714, 期待一样热爱技术的你!!!

利用cocostudio库函数 实现左右滑动的背包栏UI (cocos2d-x 2.2.0)的更多相关文章

  1. CocoStudio教程三:认识并利用CocoStudio的果实 运行2.2.1版本

    原文:CocoStudio教程三:认识并利用CocoStudio的果实 原文用的老版,用2.21搞起来好像有些问题,然后自己摸索了下,有的都是乱找的方法,只求能运行... 1,原文的CCJsonRea ...

  2. Ubuntu/Windows下利用“HIDAPI”库函数实现与Hid类USB设备通信

    一.背景: 最近在做的一个项目需要使用USB传递数据,对USB理解不是很深,USB的系统驱动编写则更是天方 夜谭,因此将设备配置成HID类设备成为了首选.其最大的好处在于,LINUX/Windows系 ...

  3. 利用selenium库自动执行滑动验证码模拟登陆

    破解流程 #1.输入账号.密码,然后点击登陆 #2.点击按钮,弹出没有缺口的图 #3.针对没有缺口的图片进行截图 #4.点击滑动按钮,弹出有缺口的图 #5.针对有缺口的图片进行截图 #6.对比两张图片 ...

  4. 利用C库函数time()打印当前系统动态时间

    引入日期和时间头文件 #include<time.h> 用time_t定义一个存储时间的变量获取时间(以秒为单位) time_t t; time (&t); //获取1970年以来 ...

  5. iOS 滑动隐藏导航栏-三种方式

    /** 1隐藏导航栏-简单- */    self.navigationController.hidesBarsOnSwipe = YES; /** 2隐藏导航栏-不随tableView滑动消失效果 ...

  6. 利用CSS预处理技术实现项目换肤功能(less css + asp.net mvc4.0 bundle)

    一.背景 在越来越重视用户体验的今天,换肤功能也慢慢被重视起来.一个web系统用户可以选择一个自己喜欢的系统主题,在用户眼里还是会多少加点分的.我们很开心的是easyui v1.3.4有自带defau ...

  7. Demon_背包系统(实现装备栏,背包栏,可以切换装备)

    using UnityEngine; using System.Collections; public enum BoxType { Normal,//普通格子 Equip//装备栏格子 } publ ...

  8. React实现顶部固定滑动式导航栏(导航条下拉一定像素时显示原导航栏样式)

    摘要 基于react的框架开发一个顶部固定滑动式的酷炫导航栏,当导航栏置顶时,导航栏沉浸在背景图片里:当鼠标滑动滚轮时,导航栏固定滑动并展示下拉样式. JS部分 相关技术栈:react.antd.re ...

  9. 利用CSS制作背景变色的横向导航栏

    1.表单 页面如下: <html> <head> <title>注册表单页面</title> </head> <body> &l ...

随机推荐

  1. wordpress中如何禁止或者屏蔽更新提示

    WordPress禁止,插件更新,主题更新,wordpress本身更新提示的方法 禁止wp更新 : open file "wordpress\wp-includes\update.php&q ...

  2. hdu2444 判断二分图+最大匹配

    #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...

  3. 【HDU 2577】How to Type

    题意 (我做了这题才知道caps lock 锁定大小写后,按一下shift键可以输入相反的大小写.) 这题就是给你只有大小写字母的字符串,求最少多少次按键盘.最后caps lock 必须是关闭的. 分 ...

  4. svn版本控制方案:多分支并行开发,多环境自动部署

    背景 keywords:svn,trunk,branch,jenkins,maven,merge 两地同时开发一个产品,目前线上有3个环境:测试环境.预发布环境.生产环境.目前系统部署采用jenkin ...

  5. rpm常用选项

    httpd-2.2.15-39.el6.centos.x86_64.rpmhttpd   -      2.2.15-    39.el6.centos.       x86_64 .rpm软件名称- ...

  6. list 和 str

    list 和 str 两种类型数据,有不少相似的地方,也有很大的区别.本讲对她们做个简要比较,同时也是对前面有关两者的知识复习一下,所谓“温故而知新”. 相同点 都属于序列类型的数据 所谓序列类型的数 ...

  7. 【uoj222】 NOI2016—区间

    http://uoj.ac/problem/222 (题目链接) 题意 有n个区间,当有m个区间有公共部分时,求m个区间长度的最大值与最小值之差的最小值. Solution 线段树+滑动窗口.这道题很 ...

  8. cogs896 圈奶牛

    描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. PROGRAM NAM ...

  9. POJ 2752 Seek the Name, Seek the Fame

    传送门 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14761   Accepted: 7407 Description ...

  10. Integer.valueOf(String) 方法之惑

    本文由 ImportNew - 靳禹 翻译自 stackoverflow.欢迎加入翻译小组.转载请见文末要求. 有个仁兄在 StackOverflow 上发起了一个问题,是这么问的: “ 我被下面的代 ...