今天要写一个滚动列表功能,类似以下这样。(图片资源都是自己从天天酷跑里面抠的,仅用于学习方便)

首先,这样一个列表就和iOS里面的UITableView没什么两样,当然,Android中肯定也存在类似的控件。

在cocos2d-x引擎中參照ios中的UITableView实现了一个叫做CCTableView的类,用于创建列表,对于熟悉ios程序设计的人来说,这个内容应该是非常好理解的。

以下就介绍下CCTableView。

首先,mark几个比較好的博文。

Cocos2d-x CCTableView实现列表:http://www.tuicool.com/articles/viaQn2

另外.先介绍下涉及的几个经常用法。

必须实现:

1
2
3
4
5
6
7
8
9
10
11
//触摸到某个cell的事件
    virtual
void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
     
    //定制每一个cell的size
    virtual
cocos2d::CCSize tableCellSizeForIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
     
    //定制每一个cell的内容
    virtual
cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
     
    //确定这个tableview的cell行数
    virtual
unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);

选择实现:

1
2
3
4
//设置单元格高亮的状态
    virtual
void tableCellHighlight(CCTableView* table, CCTableViewCell* cell);
    //设置单元格非高亮的状态
    virtual
void tableCellUnhighlight(CCTableView* table, CCTableViewCell* cell);

必须实现:

1
2
3
//因为CCTableView是继承CCScrollView,所以要继承这两个方法
    virtual
void scrollViewDidScroll(cocos2d::extension::CCScrollView* view) {}
    virtual
void scrollViewDidZoom(cocos2d::extension::CCScrollView* view) {}

以下介绍实现方法。

1。使用的时候要注意要引入扩展库文件: #include "cocos-ext.h" ,而且最好要加入�: USING_NS_CC_EXT ; 这样就不用老是加前缀cocos2d::extension。

2。要继承CCTableView的两个代理 CCTableViewDelegate 和 CCTableViewDataSource。比方:
1
2
class
HelloWorld : public cocos2d::CCLayer,public cocos2d::extension::CCTableViewDelegate,public cocos2d::extension::CCTableViewDataSource{ 
};

3。实现须要的方法(上述列举的三类中, 当中两类必须实现。 另一类可选。)

简单三不,就能定制属于你自己的列表了。非常easy吧。

以下给出上述天天酷跑道具列表的实现代码。


GameInfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// 
GameInfo.h
// 
RunGame
//
// 
Created by zhanglin on 14-05-14.
//
//
 
#ifndef
__RunGame__GameInfo__
#define
__RunGame__GameInfo__
 
#include
<iostream>
#include
"cocos2d.h"
#include
"cocos-ext.h"
USING_NS_CC_EXT;//cocos2dx定义的宏
using
namespace cocos2d;
 
class
GameInfo : public cocos2d::CCLayer,public cocos2d::extension::CCTableViewDelegate,cocos2d::extension::CCTableViewDataSource
{
public:
    virtual
bool init();
 
    static
cocos2d::CCScene* scene();
     
    void
menuCloseCallback(CCObject* pSender);
     
     
public:
     
    //CCTableViewDelegate继承自CCScrollViewDelegate
    virtual
void scrollViewDidScroll(cocos2d::extension::CCScrollView* view);
     
    virtual
void scrollViewDidZoom(cocos2d::extension::CCScrollView* view);
     
    //点击哪个cell
    virtual
void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
    //每一个cell的size
    virtual
cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);
    //生成cell
    virtual
cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
    //cell的数量
    virtual
unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);
     
    //按下去的时候,就是高亮显示,这里能够设置高亮状态
    virtual
void tableCellHighlight(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
     
    //松开的时候,取消高亮状态
    virtual
void tableCellUnhighlight(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
     
     
     
    void
scrollBar(cocos2d::extension::CCTableView* table);
 
    CREATE_FUNC(GameInfo);
};
#endif
/* defined(__RunGame__GameInfo__) */

GameInfo.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// 
GameInfo.cpp
// 
RunGame
//
// 
Created by
zhanglin on 14-05-14.
//
//
 
#include
"GameInfo.h"
#include
"SimpleAudioEngine.h"
 
using
namespace cocos2d;
using
namespace CocosDenshion;
 
CCScene*
GameInfo::scene()
{
    CCScene
*scene = CCScene::create();
     
    GameInfo
*layer = GameInfo::create();
 
    scene->addChild(layer);
     
    returnscene;
}
 
bool
GameInfo::init()
{
    if(
!CCLayer::init() )
    {
        returnfalse;
    }
     
    //获取屏幕大小
    CCSize
visibSize=CCDirector::sharedDirector()->getVisibleSize();
     
    //设置背景
    CCSprite
*bg_ = CCSprite::create(
"pic_InfoBg.png");
    this->setPosition(ccp(visibSize.width/2,
visibSize.height/2));
    this->addChild(bg_);
     
    //加入�列表
    CCTableView
*tableView=CCTableView::create(
this,
CCSizeMake(620, 450));
     
    tableView->setDirection(kCCScrollViewDirectionVertical);
     
    tableView->setPosition(ccp(-525,
-275));
     
    tableView->setAnchorPoint(ccp(0,
0));
    tableView->setDelegate(this);
     
    tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
     
    this->addChild(tableView,1);
     
    tableView->reloadData();
     
    returntrue;
}
 
void
GameInfo::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();
 
#if
(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
 
//cell的数量
unsigned
int GameInfo::numberOfCellsInTableView(CCTableView *table)
{
    return6;
}
 
//生成cell
CCTableViewCell*
GameInfo::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
    CCString
*nameString=CCString::createWithFormat(
"cell_%d.png",idx);
     
    CCTableViewCell
*cell = table->dequeueCell();
     
    if(!cell)
    {
         
        cell
=
newCCTableViewCell();
         
        cell->autorelease();
         
        //设置当前cell图片
        CCSprite
*iconSprite = CCSprite::create(nameString->getCString());
        iconSprite->setAnchorPoint(CCPointZero);
        iconSprite->setPosition(ccp(0,
0));
        iconSprite->setTag(123);
        cell->addChild(iconSprite);
         
    }
    else
    {
         
        //创建了就不须要再又一次创建了,不然你会发现图片跟文字都不正确
        CCTexture2D
*aTexture=CCTextureCache::sharedTextureCache()->addImage(nameString->getCString());
         
        CCSprite
*pSprite=(CCSprite *)cell->getChildByTag(123);
         
        pSprite->setTexture(aTexture);
 
    }
     
     
    returncell;
     
     
}
 
 
CCSize
GameInfo::cellSizeForTable(CCTableView *table)
{
    returnCCSizeMake(605,
105);
}
 
void
GameInfo::tableCellHighlight(CCTableView *table, CCTableViewCell *cell)
{
     
}
 
void
GameInfo::tableCellUnhighlight(CCTableView *table, CCTableViewCell *cell)
{
 
}
 
void
GameInfo::tableCellTouched(CCTableView *table, CCTableViewCell *cell)
{
    CCBlink
*blink_ = CCBlink::create(1.0f, 7);
    cell->runAction(blink_);
}
 
void
GameInfo::scrollViewDidScroll(cocos2d::extension::CCScrollView *view)
{
}
 
void
GameInfo::scrollViewDidZoom(cocos2d::extension::CCScrollView *view)
{
}

Cocos2d-x滚动列表具体解释(CCScrollView的使用)的更多相关文章

  1. 在滚动列表中实现视频的播放(ListView & RecyclerView)

    英文原文:Implementing video playback in a scrolled list (ListView & RecyclerView) 本文将讲解如何在列表中实现视频播放. ...

  2. Android 在滚动列表中实现视频的播放(ListView & RecyclerView)

    这片文章基于开源项目: VideoPlayerManager. 所有的代码和示例都在那里.本文将跳过许多东西.因此如果你要真正理解它是如何工作的,最好下载源码,并结合源代码一起阅读本文.但是即便是没有 ...

  3. Jquery制作--循环滚动列表

    自己模仿JQ插件的写法写了一个循环滚动列表插件,支持自定义上.下.左.右四个方向,支持平滑滚动或者间断滚动两种方式,都是通过参数设置.JQ里面有些重复的地方,暂时没想到更好的方法去精简.不过效果还是可 ...

  4. Android 高级UI设计笔记09:Android如何实现无限滚动列表

    ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们提供了一个良好,整洁的用户体验 ...

  5. 当滚动列表的时候,让input框失去焦点(移动端会收起键盘)

    1.拓展scroll.vue事件 beforeScroll:{ type:Boolean, default:false } if(this.beforeScroll){//滚动列表的时候收起键盘(移动 ...

  6. Android 高级UI设计笔记09:Android实现无限滚动列表

    1. 无限滚动列表应用场景: ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们 ...

  7. 一次react滚动列表的实践---兼容ios安卓

    一.背景 近期项目改版,对原有的h5页面进行了重新设计,数据呈现变成了瀑布流.希望新版兼容ios和安卓两端的情况下,无限制的刷新加载数据.大致效果如下: 整个页面分4部分: 顶部导航 步数状态卡片 用 ...

  8. 【js】我们需要无限滚动列表吗?

    无限滚动列表,顾名思义,是能够无限滚动的列表(愿意是指那些能够不断缓冲加载新数据的列表的).但是,我们真的需要这样一个列表吗?在PC端,浏览器的性能其实已经能够满足海量dom节点的渲染刷新(笔者经过简 ...

  9. WebApp 滚动列表的实现

    实现效果: 实现技术:overflow,flex,element::-webkit-scrollbar 实现步骤: //html:代码<div id="slider"> ...

随机推荐

  1. HDU4527+BFS

    模拟BFS搜索 对于一个将要爆炸的点,可能同时由多个点引起. /* 模拟搜索过程 */ #include<stdio.h> #include<stdlib.h> #includ ...

  2. Java泛型反射机制(一)

    /** * * @author Administrator * 功能:泛型的必要性(参数化类型)(安全简单) */ package com.test; import java.util.*; publ ...

  3. 乐1/MACBOOK/ N1 Type-C接口新体验

    经过在华强北电子市场排队一个小时,笔者顺利买到了期待已久的乐1,结合之前的NOKIA平板电脑N1,苹果全新MACBOOK,终于集齐了手机.平板.笔记本电脑三种TYPE-C接口设备(能兑换极品装备吗?^ ...

  4. A9.linux驱动

    --- ---- ---

  5. 【HDOJ】4516 威威猫系列故事——因式分解

    可解的算法太多了,采用的算法是试x的值.注意题目的输入x^3-2x^2不会写成x^3+-2x^2.一直RE在这儿. /* 4516 */ #include <iostream> #incl ...

  6. wcf service library

    创建wcf服务库的时候,系统自动生成的代码 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”. [ServiceContract] publ ...

  7. WPF——传实体类及绑定实体类属性

    public class User: private string _User; public string User1 { get { return _User; } set { _User = v ...

  8. JS中String,Math常用函数

    String对象: 1.length属性 说明:获取字符串的长度 实例: var str="abc"; var i=str.length;//output:3 2.charAt() ...

  9. Extjs4-treepanel-解析json/fields中是否加入leaf的区别

    json解析list时,我原先的写法是: Map<String, Object> map = new HashMap<String, Object>(); map.put(&q ...

  10. GML、SVG、VML的比较

    转自:http://www.cnblogs.com/3echo/archive/2009/02/19/1394399.html GML.SVG和VML都是基于XML的可用来描述矢量图形的标记语言,都是 ...