实现CCTableView带滚动条(转)
来源:互联网 发布:阿里云仓 编辑:程序博客网 时间:2024/06/01 23:42
转自:http://blog.sina.com.cn/s/blog_86ce3f9801019dd3.html
使用CCTableView可以实现拖动列表框功能,不过CCTableView没带滚动条,最近项目要求实现带滚动条,不停的尝试,基本可以满足要求了:
创建CCTableView,kCCScrollViewDirectionVertical纵向放置.
1
2
3
4
5
6
7
8
9
CCTableView* tableView = CCTableView::create(
this
, CCSizeMake(762, 439));
if
(tableView != NULL)
{
tableView->setDirection(kCCScrollViewDirectionVertical);
tableView->setPosition(ccp(33, 30));
tableView->setDelegate(
this
);
tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
this
->addChild(tableView);
}
滚动条:
1
2
3
4
5
6
7
8
CCSprite* bar = CCSprite::create(
"ui/scrollBar.png"
);
if
(bar != NULL)
{
bar->ignoreAnchorPointForPosition(
false
);
bar->setAnchorPoint(ccp(0.5, 1));
bar->setPosition(ccp(805, 465));
this
->addChild(bar, 1, 1000);
}
滚动响应事件tableCellAtIndex:
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
CCTableViewCell* Fjut::tableCellAtIndex(CCTableView* table, unsigned
int
idx)
{
CCString* string = CCString::createWithFormat(
"%d"
, idx);
CCTableViewCell* cell = table->dequeueCell();
if
(cell == NULL)
{
cell =
new
CCTableViewCell();
cell->autorelease();
// test
CCLabelTTF* label_ = CCLabelTTF::create(string->getCString(),
"Aril"
, 15);
if
(label_ != NULL)
{
label_->setPosition(ccp(65, 56));
label_->setColor(FONT_COLOR_Green);
cell->addChild(label_, 1, 100);
}
}
else
{
scrollBar(table);
// ...
CCLabelTTF* label_ = (CCLabelTTF*)cell->getChildByTag(100);
if
(label_ != NULL)
{
label_->setString(string->getCString());
}
}
return
cell;
}
处理滚动:
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
void
Fjut::scrollBar(CCTableView* table)
{
CCNode* bar =
this
->getChildByTag(1000);
if
(bar == NULL)
{
return
;
}
// tableSize.height == cell个数*cell的height
CCSize tableSize = table->getContentSize();
// CCTableView
CCSize tableViewSize = table->getViewSize();
// 每次拖动的偏移量?(负值)
CCPoint contOffsetPos = table->getContentOffset();
// 总的偏移量
float
maxOff = tableViewSize.height - tableSize.height;
// 每次拖动的偏移量
float
curOff = contOffsetPos.y - maxOff;
// 计算百分比
float
percentage =
fabs
(curOff)/
fabs
(maxOff);
printf
(
"curOff:%f, maxOff:%f, per:%f\n"
, curOff, maxOff, percentage);
// 拖拉到最顶端或最低端后继续拖动(弹回)会出现percentage值小于0.1和大于1.0的情况,我们分别把percentage重置为0和1.0f
if
(percentage < 0.1f)
{
percentage = 0;
}
if
(percentage > 1.0f)
{
percentage = 1.0f;
}
// bar移动到最顶端的position.y
float
barTopPosY = bar->getPosition().y;
// bar移动到最低端的position.y
float
barLowPosY = 75.0f;
// ....
float
h = barTopPosY - percentage*(barTopPosY- barLowPosY);;
bar->setPosition(ccp(bar->getPosition().x, h));
}
当然,滚动条的长度要根据numberOfCellsInTableView中的cell个数进行缩放。相应的barLowPosY 也跟着变化。
如图:
收工.
- 实现CCTableView带滚动条(转)
- 笨小五工作笔录之-滚动条CCTableView
- opencv实现视频播放(带滚动条)
- opencv例2.3实现视频播放(带滚动条)
- Android_RecyclerView实现上下滚动广告条(带图片)
- 实现带滚动条的dataGrid
- cocos2d-x给CCTableView加入下拉滚动条
- OpenCV 播放视频(带滚动条)
- div带滚动条
- 带滚动条TextView
- QmlTextEdit带滚动条
- cocos2dx-CCTableView滚动列表 之CCMenuItemToggle 多选实现
- opencv Trackbar实现带滚动条的视频播放,且滚动条可自动滑动
- css3--实现最小宽的的方法(带滚动条)
- JS实现GridView式的数据加载(带滚动条)
- 用层实现带横向滚动条的下拉框
- 如何实现带两个滑块的滚动条
- jsp中实现带滚动条的table表格
- iOS 滤镜 和 iOS6 中的Core Image技术
- C++ 数组和指针学习总结
- 版本控制
- 数据结构 - C语言版 - 线性表 所有基本操作
- 各种编码格式
- 实现CCTableView带滚动条(转)
- CodeForces135C Zero-One
- Unable to add window — token android.os.BinderProxy is not valid; is your activity running?
- 追击理论判断有环链表
- 停车场管理(栈和队列)
- POJ1014:Dividing
- Android 常见问题
- 数据结构 - C语言版 - 单链表 所有基本操作
- Windows下使用GSL(GNU Scientific Library)