CCScrollView在Cocos2d-X引擎中主要使用在图片尺寸远大于屏幕尺寸的时候使用。

总体来说,使用起来比较简单。

一个是CCScrollView控件本身,一个是CCScrollViewDelegate代理。

#1.现在我们先来看CCScrollView的主要方法:

 */
//滑动方向
typedef enum {
kCCScrollViewDirectionNone = -1,
kCCScrollViewDirectionHorizontal = 0,
kCCScrollViewDirectionVertical,
kCCScrollViewDirectionBoth
} CCScrollViewDirection; class CCScrollView; class CCScrollViewDelegate
{
public:
virtual ~CCScrollViewDelegate() {}
virtual void scrollViewDidScroll(CCScrollView* view) = 0; //滑动调用
virtual void scrollViewDidZoom(CCScrollView* view) = 0; //缩放调用
}; /**
* ScrollView support for cocos2d for iphone.
* It provides scroll view functionalities to cocos2d projects natively.
*/
class CCScrollView : public CCLayer
{
public:
CCScrollView();
virtual ~CCScrollView(); bool init();
virtual void registerWithTouchDispatcher(); /**
* Returns an autoreleased scroll view object.
*
* @param size view size
* @param container parent object
* @return autoreleased scroll view object
*/
static CCScrollView* create(CCSize size, CCNode* container = NULL); /**
* Returns an autoreleased scroll view object.
*
* @param size view size
* @param container parent object
* @return autoreleased scroll view object
*/
static CCScrollView* create(); /**
* Returns a scroll view object
*
* @param size view size
* @param container parent object
* @return scroll view object
*/
bool initWithViewSize(CCSize size, CCNode* container = NULL); /**
* Sets a new content offset. It ignores max/min offset. It just sets what's given. (just like UIKit's UIScrollView)
*
* @param offset new offset
* @param If YES, the view scrolls to the new offset
*/
void setContentOffset(CCPoint offset, bool animated = false);
CCPoint getContentOffset();
/**
* Sets a new content offset. It ignores max/min offset. It just sets what's given. (just like UIKit's UIScrollView)
* You can override the animation duration with this method.
* 设置新的容器坐标
* @param offset new offset
* @param animation duration
*/
void setContentOffsetInDuration(CCPoint offset, float dt); void setZoomScale(float s);
/**
* Sets a new scale and does that for a predefined duration.
* 设置CCScrollView的缩放
* @param s a new scale vale
* @param animated if YES, scaling is animated
*/
void setZoomScale(float s, bool animated); float getZoomScale(); /**
* Sets a new scale for container in a given duration.
*
* @param s a new scale value
* @param animation duration
*/
void setZoomScaleInDuration(float s, float dt);
/**
* Returns the current container's minimum offset. You may want this while you animate scrolling by yourself
*/
CCPoint minContainerOffset();
/**
* Returns the current container's maximum offset. You may want this while you animate scrolling by yourself
*/
CCPoint maxContainerOffset();
/**
* Determines if a given node's bounding box is in visible bounds
*
* @return YES if it is in visible bounds
*/
bool isNodeVisible(CCNode * node);
/**
* Provided to make scroll view compatible with SWLayer's pause method
*/
void pause(CCObject* sender);
/**
* Provided to make scroll view compatible with SWLayer's resume method
*/
void resume(CCObject* sender); bool isDragging() {return m_bDragging;}
bool isTouchMoved() { return m_bTouchMoved; }
bool isBounceable() { return m_bBounceable; } //是否开启弹性滑动,默认true,false滑动失效
void setBounceable(bool bBounceable) { m_bBounceable = bBounceable; } /**
* size to clip. CCNode boundingBox uses contentSize directly.
* It's semantically different what it actually means to common scroll views.
* Hence, this scroll view will use a separate size property.
*/
CCSize getViewSize() { return m_tViewSize; }
void setViewSize(CCSize size); CCNode * getContainer();
void setContainer(CCNode * pContainer); //设置,获取容器的一对方法 /**
* direction allowed to scroll. CCScrollViewDirectionBoth by default.
*/
CCScrollViewDirection getDirection() { return m_eDirection; }
virtual void setDirection(CCScrollViewDirection eDirection) { m_eDirection = eDirection; } //设置,获取CCScrollView的对齐方向 CCScrollViewDelegate* getDelegate() { return m_pDelegate; }
void setDelegate(CCScrollViewDelegate* pDelegate) { m_pDelegate = pDelegate; } //设置代理对象 /** override functions */
// optional
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); virtual void setContentSize(const CCSize & size); //设置容器大小
virtual const CCSize& getContentSize() const; void updateInset();
/**
* Determines whether it clips its children or not.
*/
bool isClippingToBounds() { return m_bClippingToBounds; }
void setClippingToBounds(bool bClippingToBounds) { m_bClippingToBounds = bClippingToBounds; } virtual void visit();
virtual void addChild(CCNode * child, int zOrder, int tag);
virtual void addChild(CCNode * child, int zOrder);
virtual void addChild(CCNode * child);
void setTouchEnabled(bool e);
private:
/**
* Relocates the container at the proper offset, in bounds of max/min offsets.
*
* @param animated If YES, relocation is animated
*/
void relocateContainer(bool animated);
/**
* implements auto-scrolling behavior. change SCROLL_DEACCEL_RATE as needed to choose
* deacceleration speed. it must be less than 1.0f.
*
* @param dt delta
*/
void deaccelerateScrolling(float dt);
/**
* This method makes sure auto scrolling causes delegate to invoke its method
*/
void performedAnimatedScroll(float dt);
/**
* Expire animated scroll delegate calls
*/
void stoppedAnimatedScroll(CCNode* node);
/**
* clip this view so that outside of the visible bounds can be hidden.
*/
void beforeDraw();
/**
* retract what's done in beforeDraw so that there's no side effect to
* other nodes.
*/
void afterDraw();
/**
* Zoom handling
*/
void handleZoom(); protected:
CCRect getViewRect(); /**
* current zoom scale
*/
float m_fZoomScale;
/**
* min zoom scale
*/
float m_fMinZoomScale;
/**
* max zoom scale
*/
float m_fMaxZoomScale;
/**
* scroll view delegate
*/
CCScrollViewDelegate* m_pDelegate; CCScrollViewDirection m_eDirection;
/**
* If YES, the view is being dragged.
*/
bool m_bDragging; /**
* Content offset. Note that left-bottom point is the origin
*/
CCPoint m_tContentOffset; /**
* Container holds scroll view contents, Sets the scrollable container object of the scroll view
*/
CCNode* m_pContainer;
/**
* Determiens whether user touch is moved after begin phase.
*/
bool m_bTouchMoved;
/**
* max inset point to limit scrolling by touch
*/
CCPoint m_fMaxInset;
/**
* min inset point to limit scrolling by touch
*/
CCPoint m_fMinInset;
/**
* Determines whether the scroll view is allowed to bounce or not.
*/
bool m_bBounceable; bool m_bClippingToBounds; /**
* scroll speed
*/
CCPoint m_tScrollDistance;
/**
* Touch point
*/
CCPoint m_tTouchPoint;
/**
* length between two fingers
*/
float m_fTouchLength;
/**
* UITouch objects to detect multitouch
*/
CCArray* m_pTouches;
/**
* size to clip. CCNode boundingBox uses contentSize directly.
* It's semantically different what it actually means to common scroll views.
* Hence, this scroll view will use a separate size property.
*/
CCSize m_tViewSize;
/**
* max and min scale
*/
float m_fMinScale, m_fMaxScale;
/**
* scissor rect for parent, just for restoring GL_SCISSOR_BOX
*/
CCRect m_tParentScissorRect;
bool m_bScissorRestored;
};

#2.现在看下示例代码:

.h声明
class HelloWorld : public cocos2d::CCLayer, public CCScrollViewDelegate
{
private: CCScrollView *scroll ;
float xOffSet;
float yOffSet; public: virtual bool init();
static cocos2d::CCScene* scene();
void menuCloseCallback(CCObject* pSender);
CREATE_FUNC(HelloWorld); virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view); };
.cpp实现
bool HelloWorld::init()
{
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //设置scrollView的大小,为显示的view的尺寸
scroll = CCScrollView::create(CCSizeMake(960, 640)); CCSprite *bg = CCSprite::create("11.png"); //bg->setPosition(ccp(winSize.width/2, winSize.height/2));
//容器的锚点是(0,0)
bg->setPosition(ccp(0, 0)); CCMenuItemImage *back = CCMenuItemImage::create("Icon.png", "Icon.png");
back->setPosition(CCPoint(200, 200));
bg->addChild(back); //bg->setAnchorPoint(ccp(0.5, 0.5));
//scroll->setAnchorPoint(ccp(0, 0)); //设置容器
scroll->setContainer(bg); //是开启弹性效果,关闭的话就不用使用这个控件
//scroll->setBounceable(false);
bool flag = scroll->isBounceable();
CCLog("flag: %d",flag); //设置滑动方向
//kCCScrollViewDirectionHorizontal——水平滑动
//kCCScrollViewDirectionVertical——垂直滑动
scroll->setDirection(kCCScrollViewDirectionBoth); //设置容器大小
scroll->setContentSize(CCSizeMake(978, 2189));
//触摸有效
this->setTouchEnabled(true);
CCSize scrollSize = scroll->getContentSize();
CCLog("scrollSize: %f %f",scrollSize.width,scrollSize.height); //设置代理为自身
scroll->setDelegate(this);
this->addChild(scroll); //黑边防御坐标
xOffSet = winSize.width - scrollSize.width;
yOffSet = winSize.height - scrollSize.height; return true;
} void HelloWorld::scrollViewDidScroll(CCScrollView* view)
{
static int flag = 0;
CCLog("Scroll %d",flag++); CCPoint offSet = this->scroll->getContentOffset();
CCLog("offSet : %f %f",offSet.x,offSet.y);
if (offSet.x < this->xOffSet || offSet.y < this->yOffSet) { CCLog("scrollView 已经出现黑边问题了!"); if (offSet.x < this->xOffSet ) {
CCLog("scrollView X轴 出现黑边问题了!");
this->scroll->setContentOffset(CCPoint(this->xOffSet, offSet.y));
}else{
CCLog("scrollView Y轴 已经出现黑边问题了!");
this->scroll->setContentOffset(CCPoint(offSet.x, this->yOffSet));
}
} if (offSet.x > 0 || offSet.y > 0) {
CCLog("scrollView 已经出现黑边问题了!"); if (offSet.x > 0 ) {
CCLog("scrollView X轴 出现黑边问题了!");
this->scroll->setContentOffset(CCPoint(0, offSet.y));
}else{
CCLog("scrollView Y轴 已经出现黑边问题了!");
this->scroll->setContentOffset(CCPoint(offSet.x, 0));
} } }

#3.现在,我们来看下效果

原图的尺寸是978*2189

Cocos2d—X游戏开发之CCScrollView(滑动视图)(十二)的更多相关文章

  1. Cocos2d—X游戏开发之CCToggle(菜单标签切换)CCControlSwitch(开关切换)

    Cocos2d—X游戏开发之CCToggle(菜单标签切换) 首先继承子CCMenu,是菜单标签中的一种.‘ class CC_DLL CCMenuItemToggle : public CCMenu ...

  2. Cocos2d—X游戏开发之CCTableView详解(十一)

    本来很早就想写关于CCTableView的文章,但是在基本功能实现之后呢,项目需求增加导致对这个控件的研究必须更加深入一点. 好的,现在开始介绍一下这个控件,在Cocos2d—X引擎中,这是一个仿制i ...

  3. Cocos2d—X游戏开发之VS2010 控制台输出中文,模拟器中文乱码问题解决

    首先,先解决第一个问题,我们使用VS2010开发的时候,调试的时候,中文打印出来都是乱码,这个问题很纠结. 如下图: CCLOG("cclog: 测试使用标签的自动换行和个别字体大写&quo ...

  4. [Unity3D]Unity3D游戏开发之从Unity3D到Eclipse

    ---------------------------------------------------------------------------------------------------- ...

  5. Cocos2d-x 3.x游戏开发之旅

    Cocos2d-x 3.x游戏开发之旅 钟迪龙 著   ISBN 978-7-121-24276-2 2014年10月出版 定价:79.00元 516页 16开 内容提要 <Cocos2d-x ...

  6. iOS游戏开发之UIDynamic

    iOS游戏开发之UIDynamic 简介 什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象 ...

  7. 【转载】浅谈游戏开发之2D手游工具

    浅谈游戏开发之2D手游工具 来源:http://www.gameres.com/459713.html 游戏程序 平台类型: iOS Android  程序设计: 其它  编程语言:   引擎/SDK ...

  8. [整理]Unity3D游戏开发之Lua

    原文1:[Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(上) 各位朋友,大家好,我是秦元培,欢迎大家关注我的博客,我地博客地址是blog.csdn.net/qinyuanpei.如果 ...

  9. [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘终结篇:UniLua热更新全然解读

    ---------------------------------------------------------------------------------------------------- ...

随机推荐

  1. MVC小系列(十)【PartialView中的页面重定向】

    在mvc的每个Action中,都可以指定一种返回页面的类型,可以是ActionResult,这表示返回的页面为View或者是一个PartialView, 在以Aspx为页面引擎时,PartialVie ...

  2. AppDelegate 方法详解

    iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...

  3. iOS开发——视频播放 待更新……

    本文主要实现调用系统自带的播放器,自带的播放器已经做好了屏幕的适配,集成播放,暂停,快进等功能.能够满足基本的视频播放功能及iOS的视频播放的开发. 最后将其简单的封装成一个iOS视频播放工具类. 一 ...

  4. thinkphp 行为扩展

    网站程序在运行的过程每个过程都可以看做是一种行为,例如:运行应用,加载类,执行方法,加载模板,解析模板等,也就是说,我们在程序执行过程中每个 步骤都可以 定义一些点,我们可以在运行 程序的时候 检查 ...

  5. easyui-helloworld

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 关于C++对汉字拼音的处理(3)

    之所以汉字转拼音的博文能出到3,主要是因为没有很完美的C++的解决方案,但是写到了这里可以有一个小结了. 以前的方法都有这种那种弊端,如果出现了无法识别的汉字(简体的)就无法修改处理了,但是下面的这种 ...

  7. ASP.NET错误页

    当页面发生错误的时候,ASP.Net会将错误信息展示出来,这样一来不好看,二来会泄露网站的内部实现信息,给网站带来安全隐患,因此需要定制错误页,发生错误时显示开发人员定制的页面404页面放点广告也是好 ...

  8. C++11中新特性之:unordered_map

    unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value. 不同的是unordered_map不会根据key的大小进行排序,存储时是根据key的ha ...

  9. Android学习7--日志信息的使用

    在代码的调试中,日志信息是必不可少的,关于日志信息的使用,接下来,小编简要的说明一下: 日志分为几种:Verbose:从字面上解释,verbose为冗长的,啰嗦的,所以verbose日志信息输出的内容 ...

  10. 如何在Ubuntu上创建及管理LXC容器?

    将LXC安装到Ubuntu上 $ sudo apt-get install lxc 安装完毕之后,运行lxc-checkconifg工具,检查当前Linux内核支持LXC的情况.要是一切都已被启用,内 ...