cocos2dx基础篇(16)——滚动视图CCScrollView
一、介绍
Cocos2d-x中提供了滚动视图的控件CCScrollView,可以方便地实现一些场景中需要滚动的需求。CCScrollView类继承自CCLayer,因此也可以添加其他节点作为其子节点。
二、基本使用
1.创建CCScrollView对象
CCScrollView* scrollView = CCScrollView::create();
2.设置滚动视图的大小
scrollView->setContentSize(CCSizeMake(200, 300));
3.将需要滚动的节点添加为滚动视图的子节点
CCNode* node = CCNode::create();
node->setContentSize(CCSizeMake(200, 500));
scrollView->setContainer(node);
4.设置滚动视图是否可以触控滚动
scrollView->setTouchEnabled(true);
5.添加滚动视图到场景中
addChild(scrollView);
三、常见用法
1.设置滚动方向
scrollView->setDirection(kCCScrollViewDirectionVertical);
或
scrollView->setDirection(kCCScrollViewDirectionHorizontal);
2.设置滚动条是否可见
scrollView->setVerticalScrollBarEnabled(false);
scrollView->setHorizontalScrollBarEnabled(false);
3.设置滚动条的样式
scrollView->setVerticalScrollBar(pScrollBar);
scrollView->setHorizontalScrollBar(pScrollBar);
4.设置滑动边缘是否有回弹效果
scrollView->setBounceable(true);
5.设置滑动响应的缓冲时间
scrollView->setBounceDuration(0.5f);
6.设置滚动位置
scrollView->setContentOffset(ccp(0, -200), true);
四、进阶用法
1.监听滚动事件
scrollView->setDelegate(this);
2.实现回调方法
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view);
3.具体实现
void SomeClass::scrollViewDidScroll(CCScrollView* view)
{
CCLOG("scrolling...");
}
享受编程的乐趣吧!
小编注:以上内容深度分享自企鹅辅导《Lua 2D游戏开发》课程,干货满满。
