循环的pageview

在平常偶尔能看到一些前辈的优秀的代码,但是一直苦于没有地方保存,之后我就把它放到这里保存着

cocos2dx 循环翻页的pageview。

引擎中提供的pageview没有循环的功能,这个类实现了循环,而且即使更新,也就是不预先创建大量的页面,这样稍微节省点内存资源。下面是代码。

头文件:

#ifndef __CyclePageView_H__
#define __CyclePageView_H__
#include "ui/UIPageView.h"
NS_CC_BEGIN

namespace ui {

//
class CCyclePageView : public PageView
{
public:
	static CCyclePageView* create(); 

	void SetPageSize(ssize_t size);
	 
	void scrollToPage(ssize_t idx) ; 

	void CopyProp(PageView* pPageView);
	 
	Layout* GetCurPage(){ return m_pCurLayout;}

	void addPageChangedListener(const std::function<void(Layout*, size_t)> callback){ m_callback = callback; }

protected:
	virtual bool onTouchBegan(Touch *touch, Event *unusedEvent) override;

	virtual bool scrollPages(Vec2 touchOffset) override;

	virtual void handleMoveLogic(Touch *touch) override;

	virtual void handleReleaseLogic(Touch *touch) override;
	
	void movePages(Vec2 offset);
	  
	void updateAllPagesSize();

	void updateAllPagesPosition();
	
	virtual void doLayout() override;
	
	void UpdateShowLayout(ssize_t nCurIdx, Layout* layout, bool isForceCallBack=false);

	CCyclePageView();
	~CCyclePageView();


	Layout* m_pLeftLayout;
	Layout* m_pRightLayout;
	Layout* m_pCurLayout;

	ssize_t m_nPageSize;

	std::function<void(Layout*,size_t)> m_callback;
};


}
NS_CC_END
#endif // __CyclePageView_H__
cpp文件:
<pre name="code" class="cpp">#include "CyclePageView.h" 

NS_CC_BEGIN

namespace ui {


CCyclePageView* CCyclePageView::create()
{
	CCyclePageView* widget = new (std::nothrow) CCyclePageView();
	if (widget && widget->init())
	{
		widget->autorelease();
		return widget;
	}
	CC_SAFE_DELETE(widget);
	return nullptr;
}

CCyclePageView::CCyclePageView() :m_pLeftLayout(nullptr), m_pRightLayout(nullptr),
 m_pCurLayout(nullptr), m_callback(nullptr), m_nPageSize(0)
{
	_touchEnabled = false;
	_isAutoScrolling = false;
	_curPageIdx = 0;
}
CCyclePageView::~CCyclePageView()
{
}


void CCyclePageView::SetPageSize(ssize_t size)
{
	if (!m_pCurLayout)
	{
		m_pCurLayout = Layout::create();
		m_pCurLayout->setContentSize(getContentSize());
		addChild(m_pCurLayout);
	}
	if (!m_pRightLayout)
	{
		m_pRightLayout = Layout::create();
		m_pRightLayout->setContentSize(getContentSize());
		addChild(m_pRightLayout);
	}
	if (!m_pLeftLayout)
	{
		m_pLeftLayout = Layout::create();
		m_pLeftLayout->setContentSize(getContentSize());
		addChild(m_pLeftLayout);
	}
	m_nPageSize = size;
}

void CCyclePageView::scrollToPage(ssize_t idx)
{
	if (_direction == Direction::HORIZONTAL)
	{
		_autoScrollDistance = (_curPageIdx - idx)*getContentSize().width;
		if (m_pCurLayout)
		{
			_autoScrollDistance -= m_pCurLayout->getPositionX();
		}
		if (_autoScrollDistance > 0)
		{
			_autoScrollDirection = AutoScrollDirection::RIGHT;
			_touchMoveDirection = TouchDirection::RIGHT;
			UpdateShowLayout(_curPageIdx - 1, m_pRightLayout);
		}
		else
		{
			_autoScrollDirection = AutoScrollDirection::LEFT;
			_touchMoveDirection = TouchDirection::LEFT;
			UpdateShowLayout(_curPageIdx + 1, m_pLeftLayout);
		}
	}
	else if (_direction == Direction::VERTICAL)
	{
		_autoScrollDistance = (_curPageIdx - idx)*getContentSize().height;
		if (m_pCurLayout)
		{
			_autoScrollDistance -= m_pCurLayout->getPositionY();
		}
		if (_autoScrollDistance > 0)
		{
			_autoScrollDirection = AutoScrollDirection::DOWN;
			_touchMoveDirection = TouchDirection::DOWN;
			UpdateShowLayout(_curPageIdx - 1, m_pRightLayout);
		}
		else
		{
			_autoScrollDirection = AutoScrollDirection::UP;
			_touchMoveDirection = TouchDirection::UP;
			UpdateShowLayout(_curPageIdx + 1, m_pLeftLayout);
		}
	}
	_autoScrollSpeed = fabs(_autoScrollDistance) / 0.2f;
	if (_autoScrollDistance>0.000001f || _autoScrollDistance<-0.000001f)
	{
		_isAutoScrolling = true;
	}
	UpdateShowLayout(_curPageIdx, m_pCurLayout,true);
}
 
bool CCyclePageView::scrollPages(Vec2 touchOffset)
{
	if (m_nPageSize <= 0)
	{
		return false;
	}
	movePages(touchOffset);
	return true;
}

void CCyclePageView::movePages(Vec2 offset)
{
	if (!m_pCurLayout || !m_pRightLayout || !m_pLeftLayout)
	{
		return;
	}
	m_pCurLayout->setPosition(m_pCurLayout->getPosition() + offset);
	m_pRightLayout->setPosition(m_pRightLayout->getPosition() + offset);
	m_pLeftLayout->setPosition(m_pLeftLayout->getPosition() + offset);
	Size size = getContentSize();
	switch (_touchMoveDirection)
	{
	case TouchDirection::LEFT: // left 
		if (m_pCurLayout->getPositionX()<-size.width*0.75)
		{

			Layout* newRight = m_pLeftLayout;
			newRight->setPositionX(m_pRightLayout->getPositionX() + size.width);
			m_pLeftLayout = m_pCurLayout;
			m_pCurLayout = m_pRightLayout;
			m_pRightLayout = newRight;

			++_curPageIdx;
			UpdateShowLayout(_curPageIdx, m_pCurLayout,true);
		}

		break;
	case TouchDirection::RIGHT: // right 
		if (m_pCurLayout->getPositionX()>size.width*0.75)
		{
			Layout* newLeft = m_pRightLayout;
			newLeft->setPositionX(m_pLeftLayout->getPositionX() - size.width);
			m_pRightLayout = m_pCurLayout;
			m_pCurLayout = m_pLeftLayout;
			m_pLeftLayout = newLeft;
			
			--_curPageIdx;
			UpdateShowLayout(_curPageIdx, m_pCurLayout, true);
		}
		break;
	case TouchDirection::UP:
		if (m_pCurLayout->getPositionY()>size.width*0.75)
		{
			Layout* newLeft = m_pRightLayout;
			newLeft->setPositionX(m_pLeftLayout->getPositionX() - size.height);
			m_pRightLayout = m_pCurLayout;
			m_pCurLayout = m_pLeftLayout;
			m_pLeftLayout = newLeft;
			--_curPageIdx;
			UpdateShowLayout(_curPageIdx, m_pCurLayout, true);
		}
		break;
	case TouchDirection::DOWN:
		if (m_pCurLayout->getPositionY()<-size.height*0.75)
		{
			Layout* newRight = m_pLeftLayout;
			newRight->setPositionX(m_pRightLayout->getPositionX() + size.height);
			m_pLeftLayout = m_pCurLayout;
			m_pCurLayout = m_pRightLayout;
			m_pRightLayout = newRight;
			++_curPageIdx;
			UpdateShowLayout(_curPageIdx, m_pCurLayout, true);
		}
		break;
	default:
		break;
	}
	if (_curPageIdx<0)
	{
		_curPageIdx = m_nPageSize-1;
	}
	if (_curPageIdx >= m_nPageSize)
	{
		_curPageIdx = 0;
	}
}

void CCyclePageView::doLayout()
{
	if (!_doLayoutDirty)
	{
		return;
	}

	updateAllPagesPosition();
	updateAllPagesSize();
	//updateBoundaryPages();
	 
	_doLayoutDirty = false;
}
void CCyclePageView::updateAllPagesSize()
{
	Size selfSize = getContentSize();
	if (!m_pCurLayout || !m_pRightLayout || !m_pLeftLayout)
	{
		return;
	}
	m_pCurLayout->setContentSize(selfSize);
	m_pRightLayout->setContentSize(selfSize);
	m_pLeftLayout->setContentSize(selfSize);
}

void CCyclePageView::updateAllPagesPosition()
{  
	// If the layout is dirty, don't trigger auto scroll
	_isAutoScrolling = false;

	if (!m_pCurLayout || !m_pRightLayout || !m_pLeftLayout)
	{
		return;
	}
	Size size = getContentSize();
	m_pCurLayout->setPosition(Vec2::ZERO);
	if (_direction == Direction::HORIZONTAL)
	{
		m_pRightLayout->setPositionX(size.width);
		m_pLeftLayout->setPositionX(-size.width);
	}
	else if (_direction == Direction::VERTICAL)
	{
		m_pRightLayout->setPositionY(size.height);
		m_pLeftLayout->setPositionY(-size.height);
	} 
}

void CCyclePageView::CopyProp(PageView* pPageView)
{
	if (pPageView)
	{
		this->copyProperties(pPageView);
		this->copyClonedWidgetChildren(pPageView);
		this->copySpecialProperties(pPageView);
	}
}
bool CCyclePageView::onTouchBegan(Touch *touch, Event *unusedEvent)
{
	return  PageView::onTouchBegan(touch, unusedEvent);
}

void CCyclePageView::handleMoveLogic(Touch *touch)
{
	PageView::handleMoveLogic(touch);
	UpdateShowLayout(_curPageIdx, m_pCurLayout);
	switch (_touchMoveDirection)
	{
	case TouchDirection::LEFT: // left 
		UpdateShowLayout(_curPageIdx+1, m_pRightLayout);
		break;
	case TouchDirection::RIGHT: // right 
		UpdateShowLayout(_curPageIdx- 1, m_pLeftLayout);
		break;
	case TouchDirection::UP:
		UpdateShowLayout(_curPageIdx - 1, m_pLeftLayout);
		break;
	case TouchDirection::DOWN:
		UpdateShowLayout(_curPageIdx + 1, m_pRightLayout);
		break;
	default:
		break;
	}
}
void CCyclePageView::UpdateShowLayout(ssize_t nCurIdx, Layout* layout,bool isForceCallBack)
{
	if (nCurIdx<0)
	{
		nCurIdx = m_nPageSize - 1;
	}
	if (nCurIdx >= m_nPageSize)
	{
		nCurIdx = 0;
	}
	if (layout && (isForceCallBack || layout->getTag() != nCurIdx))
	{ 
		layout->setTag(nCurIdx);
		if (m_callback)
		{
			m_callback(layout,nCurIdx);
		}
	}
}

void CCyclePageView::handleReleaseLogic(Touch *touch)
{
	if (m_nPageSize <= 0)
    {
        return;
    }
	if (m_pCurLayout)
    {

	Vec2 curPagePos = m_pCurLayout->getPosition();
	auto contentSize = getContentSize();

	float moveBoundray = 0.0f;
	float scrollDistance;
	if (_direction == Direction::HORIZONTAL)
	{
		curPagePos.y = 0;
		moveBoundray = curPagePos.x;
		scrollDistance = contentSize.width / 2.0;
	}
	else if (_direction == Direction::VERTICAL)
	{
		curPagePos.x = 0;
		moveBoundray = curPagePos.y;
		scrollDistance = contentSize.height / 2.0;
	}

	if (!_usingCustomScrollThreshold)
	{
		_customScrollThreshold = scrollDistance;
	}
	float boundary = _customScrollThreshold;

	if (_direction == Direction::HORIZONTAL)
	{
		if (moveBoundray <= -boundary)
		{
			scrollToPage(_curPageIdx + 1);
		}
		else if (moveBoundray >= boundary)
		{
			scrollToPage(_curPageIdx - 1);
		}
		else
		{
			scrollToPage(_curPageIdx);
		}
	}
	else if (_direction == Direction::VERTICAL)
	{
		if (moveBoundray >= boundary)
		{
			scrollToPage(_curPageIdx + 1);
		}
		else if (moveBoundray <= -boundary)
		{
			scrollToPage(_curPageIdx - 1);
		}
		else
		{
			scrollToPage(_curPageIdx);
		}
	}
    }
}
}
NS_CC_END
</pre><pre name="code" class="cpp">
很好理解,直接放到引擎的ui里面,直接用就是了。如果js要用,那还要自己手动绑定,这个按照引擎绑定照着做就行。来源地址: https://github.com/mydishes/cocos2dx-Ex/tree/master/CyclePageView
 

 


源码KIPageView,UITableView 很强大,可是只能竖向滚动;UICollectionView 可以解决各种布局难题,但是稍显复杂,对于一些简单的需求,有点杀鸡用牛刀的感觉。 在 iOS6 以前,还没有 UICollectionView,为了实现横向滚动的 UITableView,只有自己动手写组件。为了达到和 UITableView 差不多的效果,就得先弄清其内部实现机制是怎么回事。 在渲染 View 的时候,是很耗系统资源的,如果创建大量的 View, 系统运行将变得异常缓慢,甚至导致内存耗尽。但是,在实际应用中,我们难免会遇到大量的数据需要显示,如果每显示一个数据,我们都创建一个 View,那应用程序的体验将相当糟糕。所以 Apple 为 iOS 开发者提供了 UITableView,Google 为 Android 开发者提供了 ListView。 简单来讲,UITableView 采用复用机制,其只会显示其可见区域内的 UITableViewCell。我们在滑动的过程中,当超出 UITableView 可见区域的 Cell,将会从 UITableView 中移除,并加入回收池中以作复用。当 UITableView 需要显示新的 Cell,会先从回收池中查找是否有相应的 Cell 可以重用(通过 dequeueReusableCellWithIdentifier:)。如果有,则直接将其重新显示;如果没有,则创建新的 Cell。这样一来,就可以避免因创建过多的 View,导致内存耗尽的尴尬情况。 了解了其内部的运行原理,我们也可以实现一个自己的 UITableView。 很常见的一个应用场景——显示图片:如果显示一张图片,我们用一个 UIImageView 足矣,如果要显示多张图片,并且可以左右滚动,最简单的办法是用一个 UIScrollView 包含多个 UIImageView, 但是这样带来的后果则是,如果图片数据量较大,那这个程序根本没有办法正常使用。如果我们还需要实现无限循环滚动,那这个解决方案肯定是不行的。所以这时候,就得我们自己实现一个 UITableView。 最开始,我写了一个组件叫 KIFlowView,实现了上面讲的需求,但是都是 iOS5 时代的产物了,难免过于陈旧。在后续的工作中也发现,类似的需求其实挺多的,比如左右滑动的 View,如网易新闻客户端,可以左右滑动,在不同的新闻栏目之间进行切换;有时候我们也需要实现一些 Tab,如果 Tab 的项目比较多,也需要考虑复用的问题,所以决定重新写一个增强组件,作为其替代品,所以就产生了 KIPageView。 测试环境:Xcode 6.2,iOS 6.0 以上
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值