ScrollView的使用
2015-12-26 13:26
337 查看
基本说明一下吧:该控件是cocos2d-x的扩展控件,主要使用在显示内容上,并且是那种需要拖动显示的地方,比如,使用说明这个地方,最容易使用到。好了,上代码吧。
ScrollScene.h
#ifndef __SCROLL_SCENE__
#define __SCROLL_SCENE__
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
class ScrollScene : public CCLayer, public extension::CCScrollViewDelegate{
public:
ScrollScene();
virtual bool init();
static CCScene* scene();
virtual void scrollViewDidScroll(extension::CCScrollView* view);
virtual void scrollViewDidZoom(extension::CCScrollView* view);
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
CREATE_FUNC(ScrollScene);
private:
CCSize winSize;
CCPoint centerCcp;
extension::CCScrollView* scrollView;
short curScrollIndex;
};
#endif
ScrollScene.cpp
#include "ScrollScene.h"
#include "cocos-ext.h"
#define BG_WIDTH 480
#define BG_HEIGHT 320
ScrollScene::ScrollScene() {
winSize = CCDirector::sharedDirector()->getWinSize();
centerCcp = CCPoint(winSize.width/2, winSize.height/2);
curScrollIndex = 0;
}
bool ScrollScene::init() {
CCLayer::init();
scrollView = extension::CCScrollView::create();//创建一个scrollView
CCLayer* containerLayer = CCLayer::create();
CCSprite *sprite1 = CCSprite::create("imageA.png");
sprite1->setPosition(ccp(240 + 0 * 480, 160));
containerLayer->addChild(sprite1);
CCSprite *sprite2 = CCSprite::create("imageB.png");
sprite2->setPosition(ccp(240 + 1 * 480, 160));
containerLayer->addChild(sprite2);
CCSprite* sprite3 = CCSprite::create("HelloWorld.png");
sprite3->setPosition(ccp(240 + 2*480, 160));
containerLayer->addChild(sprite3);
containerLayer->setAnchorPoint(CCPointZero);
containerLayer->setPosition(CCPointZero);
scrollView->setAnchorPoint(CCPointZero);
scrollView->setPosition(CCPointZero);
// 显示显示的区域
scrollView->setViewSize(CCSizeMake(480, 320));//设置view的大小
scrollView->setContentOffset(CCPointZero);
containerLayer->setContentSize(CCSizeMake(1440, 320));//设置滚动区域的大小
// 显示滑动的区域大小 scrollview的实际大小
scrollView->setContentSize(CCSizeMake(1440, 320));//设置scrollview区域的大小
scrollView->setContainer(containerLayer); //设置需要滚动的内容
//因为要自己实现触摸消息,所以这里设为false scrollView->setTouchEnabled(false);
scrollView->setDirection(extension::kCCScrollViewDirectionHorizontal); //设置滚动的方向,有三种可以选择
scrollView->setDelegate(this);
this->addChild(scrollView);
scrollView->setScaleX(winSize.width/BG_WIDTH);
scrollView->setScaleY(winSize.height/BG_HEIGHT);
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, true, this);
return true;
}
CCScene* ScrollScene::scene() {
CCScene* scene = CCScene::create();
ScrollScene* layer = ScrollScene::create();
scene->addChild(layer);
return scene;
}
// 这两个方法,只需要实现就好,不用写任何操作
void ScrollScene::scrollViewDidScroll(extension::CCScrollView* view) {
}
void ScrollScene::scrollViewDidZoom(extension::CCScrollView* view) {
}
bool ScrollScene::ccTouchBegan(CCTouch* touch, CCEvent* event) {
CCPoint currentCcp = touch->getLocation();
if (!scrollView->boundingBox().containsPoint(currentCcp)) {
return false;
}
return true;
}
void ScrollScene::ccTouchMoved(CCTouch* touch, CCEvent* event) {
}
void ScrollScene::ccTouchEnded(CCTouch* touch, CCEvent* event) {
CCPoint startCcp = touch->getStartLocation();
CCPoint endCcp = touch->getLocation();
CCPoint adjustCcp = scrollView->getContentOffset();
if (endCcp.x - startCcp.x > 0 && curScrollIndex > 0) {
adjustCcp = ccpSub(scrollView->getContentOffset(), ccp(-480, 0));
curScrollIndex--;
}
if (endCcp.x - startCcp.x < 0 && curScrollIndex < 2) {
adjustCcp = ccpSub(scrollView->getContentOffset(), ccp(480, 0));
curScrollIndex++;
}
// 这里把时间设置为0.2f是有含义的,如果将时间设置的过长,在用户滑动过快的过程中,会连续触发该方法///,会导致视图出错,如果实在是想要把时间调整的大一点,比如1.0f,可以这是一个状态,判断是否在scrollView是否//在滑动过程中,在判断处理一下即可!(这里为了省劲,就直接写了一个相对较小的时间段);
scrollView->setContentOffsetInDuration(adjustCcp, 0.2f);
}
ScrollScene.h
#ifndef __SCROLL_SCENE__
#define __SCROLL_SCENE__
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
class ScrollScene : public CCLayer, public extension::CCScrollViewDelegate{
public:
ScrollScene();
virtual bool init();
static CCScene* scene();
virtual void scrollViewDidScroll(extension::CCScrollView* view);
virtual void scrollViewDidZoom(extension::CCScrollView* view);
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
CREATE_FUNC(ScrollScene);
private:
CCSize winSize;
CCPoint centerCcp;
extension::CCScrollView* scrollView;
short curScrollIndex;
};
#endif
ScrollScene.cpp
#include "ScrollScene.h"
#include "cocos-ext.h"
#define BG_WIDTH 480
#define BG_HEIGHT 320
ScrollScene::ScrollScene() {
winSize = CCDirector::sharedDirector()->getWinSize();
centerCcp = CCPoint(winSize.width/2, winSize.height/2);
curScrollIndex = 0;
}
bool ScrollScene::init() {
CCLayer::init();
scrollView = extension::CCScrollView::create();//创建一个scrollView
CCLayer* containerLayer = CCLayer::create();
CCSprite *sprite1 = CCSprite::create("imageA.png");
sprite1->setPosition(ccp(240 + 0 * 480, 160));
containerLayer->addChild(sprite1);
CCSprite *sprite2 = CCSprite::create("imageB.png");
sprite2->setPosition(ccp(240 + 1 * 480, 160));
containerLayer->addChild(sprite2);
CCSprite* sprite3 = CCSprite::create("HelloWorld.png");
sprite3->setPosition(ccp(240 + 2*480, 160));
containerLayer->addChild(sprite3);
containerLayer->setAnchorPoint(CCPointZero);
containerLayer->setPosition(CCPointZero);
scrollView->setAnchorPoint(CCPointZero);
scrollView->setPosition(CCPointZero);
// 显示显示的区域
scrollView->setViewSize(CCSizeMake(480, 320));//设置view的大小
scrollView->setContentOffset(CCPointZero);
containerLayer->setContentSize(CCSizeMake(1440, 320));//设置滚动区域的大小
// 显示滑动的区域大小 scrollview的实际大小
scrollView->setContentSize(CCSizeMake(1440, 320));//设置scrollview区域的大小
scrollView->setContainer(containerLayer); //设置需要滚动的内容
//因为要自己实现触摸消息,所以这里设为false scrollView->setTouchEnabled(false);
scrollView->setDirection(extension::kCCScrollViewDirectionHorizontal); //设置滚动的方向,有三种可以选择
scrollView->setDelegate(this);
this->addChild(scrollView);
scrollView->setScaleX(winSize.width/BG_WIDTH);
scrollView->setScaleY(winSize.height/BG_HEIGHT);
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, true, this);
return true;
}
CCScene* ScrollScene::scene() {
CCScene* scene = CCScene::create();
ScrollScene* layer = ScrollScene::create();
scene->addChild(layer);
return scene;
}
// 这两个方法,只需要实现就好,不用写任何操作
void ScrollScene::scrollViewDidScroll(extension::CCScrollView* view) {
}
void ScrollScene::scrollViewDidZoom(extension::CCScrollView* view) {
}
bool ScrollScene::ccTouchBegan(CCTouch* touch, CCEvent* event) {
CCPoint currentCcp = touch->getLocation();
if (!scrollView->boundingBox().containsPoint(currentCcp)) {
return false;
}
return true;
}
void ScrollScene::ccTouchMoved(CCTouch* touch, CCEvent* event) {
}
void ScrollScene::ccTouchEnded(CCTouch* touch, CCEvent* event) {
CCPoint startCcp = touch->getStartLocation();
CCPoint endCcp = touch->getLocation();
CCPoint adjustCcp = scrollView->getContentOffset();
if (endCcp.x - startCcp.x > 0 && curScrollIndex > 0) {
adjustCcp = ccpSub(scrollView->getContentOffset(), ccp(-480, 0));
curScrollIndex--;
}
if (endCcp.x - startCcp.x < 0 && curScrollIndex < 2) {
adjustCcp = ccpSub(scrollView->getContentOffset(), ccp(480, 0));
curScrollIndex++;
}
// 这里把时间设置为0.2f是有含义的,如果将时间设置的过长,在用户滑动过快的过程中,会连续触发该方法///,会导致视图出错,如果实在是想要把时间调整的大一点,比如1.0f,可以这是一个状态,判断是否在scrollView是否//在滑动过程中,在判断处理一下即可!(这里为了省劲,就直接写了一个相对较小的时间段);
scrollView->setContentOffsetInDuration(adjustCcp, 0.2f);
}
相关文章推荐
- 用Python脚本做一些网页游戏中力所能及的自动化任务
- 马哥linux学习笔记:centos7中的yum详解
- 防尘和防水等级
- C语言学习笔记----伊能C语言学习笔记----变量可以在程序的三个地方声明
- LWIP 移植文件内容
- 使用Python实现Hadoop MapReduce程序
- Floyd算法(多源最短路算法)
- 文本主题特征抽取实践与构想
- CSS marquee滚动 长宽怎么设置代码
- Hibernate 学习代码/笔记(四)(一对多)
- android笔记-1
- C语言学习笔记----伊能C语言学习笔记----如何在一个文件中使用另一个文件中定义的变量呢??
- hdu 2710 Max Factor
- tomcatserver解析(六)-- Acceptor
- Scrollview.pageview和listview的使用
- Visual Studio 更优雅的使用
- iOS应用笔记之git的本地使用
- PAT乙级 有几个PAT(25)
- 列式存储
- 自定义圆角点击变色TextView