quick cocos UIListView之isItemInViewRect方法修正

功能描述一个滚动列表,当列表可视区域上部有内容时则上部出现向上箭头提示,当列表可视区域下部有内容则下部出现向下箭头提示

功能实现:应用cocos studio1.6制作界面,上面放置一个背景,一个滚动列表,然后程序加载解析这个界面的json文件,应用quick3.3final下的UIListView的方法isItemInViewRect进行检测第一条与最后一条是否在可视区域内。

问题:当界面加载进来,坐标设置0,0时,isItemInViewRect方法判断都没问题,但当把界面调整位置时,isItemInViewRect方法就不能准确判断某一条目是否在列表可视区域内了

问题解决究其原因,发现isItemInViewRect方法现实如下

function UIListView:isItemInViewRect(pos)
	local item
	if "number" == type(pos) then
		item = self.items_[pos]
	elseif "userdata" == type(pos) then
		item = pos
	end

	if not item then
		return
	end
	
	local bound = item:getBoundingBox()
	local nodePoint = self.container:convertToWorldspace(
		cc.p(bound.x,bound.y))
	bound.x = nodePoint.x
	bound.y = nodePoint.y

	return cc.rectIntersectsRect(self.viewRect_,bound)
end

从实现看,首先把列表条目item转化为世界坐标,然后再判断列表可视区域viewRect_是否包含条目矩形区域,由此问题来了,列表条目被转化为了世界坐标,但列表可视区域viewRect_的坐标并没有转化为世界坐标系,这就肯定会出问题,不知道quick这样设计的初衷是什么,现把该方法修正如下:

function UIListView:isItemInViewRect(pos)
	local item
	if "number" == type(pos) then
		item = self.items_[pos]
	elseif "userdata" == type(pos) then
		item = pos
	end

	if not item then
		return
	end
	
	local bound = item:getBoundingBox()
	local nodePoint = self.container:convertToWorldspace(
		cc.p(bound.x,bound.y))
	bound.x = nodePoint.x
	bound.y = nodePoint.y
    
    local viewRectPos = self:convertToWorldspace(cc.p(self.viewRect_.x,self.viewRect_.y))
    local viewRect = cc.rect(viewRectPos.x,viewRectPos.y,self.viewRect_.width,self.viewRect_.height)
    
    return cc.rectIntersectsRect(viewRect,bound)
end

主要是把viewRect_也转化为世界坐标再进行区域交互判断即可,望对有同样问题的同胞有所帮助

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...