cocos2d-x 源码 :可以循环的CCScrollView (代码已经重构过,附使用方法)

本文介绍了cocos2d-x中实现可循环滚动的CCScrollView源码,详细讲解了准备工作、源码结构及使用方法,帮助开发者更好地理解和应用这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cocos2d-x源码总目录

http://blog.youkuaiyun.com/u011225840/article/details/31743129

1.准备工作

想弄懂可循环的CCscrollView,首先请阅读cocos2d-x本身的CCscrollView源码http://blog.youkuaiyun.com/u011225840/article/details/30033501(我已经添加注释,方便阅读)。

2.源码展示

因为源码我想放到git上,所以注释都是用的英文,如果这部分源码有人有问题,请在评论区留言,我会逐一回答。
总体说下,CCCycleScrollview继承了CCscrollView以及CCscrollViewDelegate,这样保证了代码的可扩展性。
同时,CCCycleScrollview参考了tableView,使用了CCCycleCell,将部分界面和数据部分解耦。
/****************************************************************************
 Author : poplaryang 
		  2014- 06
 
 http://blog.youkuaiyun.com/u011225840

 
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 ****************************************************************************/

#include "cocos2d.h"
#include "cocos-ext.h"
#include <vector>
using namespace cocos2d;
using namespace cocos2d::extension;

#define SCROLL_DEACCEL_RATE  0.95f
#define SCROLL_DEACCEL_DIST  1.0f

/*
	abstract class 
	the class defines that the what a cycleCell needs to do.
*/
class CCCycleCell :public CCNode
{
public:
	//The cell has been selected ,so you can do something with that, Like  highlight or what ever you want.
	virtual void getSelected() = 0;
	//The cell must show the different view with the different index. Like, the hours within 12.
	virtual void updateWithIndex(unsigned int index) = 0;

	unsigned int getIndex(){return m_index;}



protected:
	unsigned int m_index; 
};

//The moving direction 
enum MovingDirection
{
	Left = 1,
	Right = 2,
	Up,
	Down
};

//The direction that the view allowd to move, the view can't do the both like CCScrollView.
enum Direction
{
	CycleDirectionHorizontal=1,
	CycleDirectionVertical

};

/*
	The view that can show the data with a cycle way.(forgive my awful English ....)
	Eg. The clock the view in iphone, the view will show 0 after 23,otherwise ,you move the opposite direction 23,22,21....until 0.
	
*/
class CCCycleScrollView: public CCScrollView,public CCScrollViewDelegate
{
public:
	CCCycleScrollView();
	virtual ~CCCycleScrollView();

	/*
		The background node is the background that will be show cyclely.
		The nodeCount is the count that every background node can hold the cycleCell.
		The totalCount is the count that how many different data in the cycle cell.Eg...The hour in a day ,can have the 24 totalCount.
	*/
	static CCCycleScrollView* create(std::vector<CCNode*>& background,std::vector<CCCycleCell*>& cycleCell,unsigned int nodeCount =0,unsigned int totalCount = 0,Direction direction = CycleDirectionHorizontal);

	/*
		init methods.
	*/
	bool initWithViewSize(std::vector<CCNode*>& background,std::vector<CCCycleCell*>& cycleCell,unsigned int nodeCount =0,unsigned int totalCount = 0,Direction direction = CycleDirectionHorizontal);
	
	/*
		TouchDelegate.
	*/
	bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
	void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
	void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
	void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
protected:
	
	//CCScrollViewDelegate
	virtual void scrollViewDidScroll(CCScrollView* view);
	virtual void scrollViewDidZoom(CCScrollView* view);
	virtual void scrollViewDidStop(CCScrollView* view);

	//The delegate must be self, so beyound the class ,anyone can't change the scrollview delegate.
	void setDelegate(){};


	/*
		we should adjust the backgroundNode's posi
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值