ListView:addScrollViewEventListener( function ( sender,eventType )
if evenType == ccui.ScrollviewEventType.scrollToBottom then
print( "callback func" );
end
end );
然后当滑动到最下面的时候就一直打印一直打印。。。蛋疼。
于是我便相处了另一个方案:利用监听点击事件去处理这个逻辑
ListView:addTouchEventListener(function (sender,eventType)
if eventType == ccui.TouchEventType.ended then
local eInnerPos = ListView:getInnerContainerPosition();
--// 偏移量 20 个像素
if eInnerPos.y >= 20 then
print( "------ call back function" );
end
end
end );
还有一个一劳永逸的方法,给 scrollview 加一个成员函数。在 UIScrollView.lua 文件里的 scrollview 类加入成员函数
function ScrollView:addScrollViewMoveSideCallFunc( callback ) local InnerSize = self:getInnerContainerSize(); local ContentSize = self:getContentSize(); self:addTouchEventListener( function ( sender,TouchEventType ) if TouchEventType == ccui.TouchEventType.ended then local eInnerPos = self:getInnerContainerPosition(); local topOffY = ((InnerSize.height-ContentSize.height)*-1); if eInnerPos.y <= topOffY-20 then callback( "SCROLL_TO_TOP" ); elseif eInnerPos.y >= 20 then callback( "SCROLL_TO_BOTTOM" ); end end end ); end
可恨啊。放进去懂东西把点击事件给拦截了。。。。导致触发不了。。。于是临时想了另一种方法,在list的父控件上加一个跟list大小一样位置一样的 layout。点击事件在 layout 上去触发。
function ScrollView:addScrollViewMoveSideCallFunc( callback ) local ContentSize = self:getContentSize(); local EventLayout = ccui.Layout:create(); EventLayout:setTouchEnabled( true ); EventLayout:setContentSize( ContentSize ); local Parent = self:getParent(); local svPosX = self:getPositionX(); local svPosY = self:getPositionY(); EventLayout:setPosition( svPosX - ContentSize.width / 2,svPosY - ContentSize.height / 2 ); Parent:addChild( EventLayout,self:getLocalZOrder() ); EventLayout:setSwallowTouches( false ); EventLayout:addTouchEventListener( function ( sender,TouchEventType ) if TouchEventType == ccui.TouchEventType.ended then local InnerSize = self:getInnerContainerSize(); local eInnerPos = self:getInnerContainerPosition(); local topOffY = ((InnerSize.height-ContentSize.height)*-1); if eInnerPos.y <= topOffY-20 then callback( "SCROLL_TO_TOP" ); print("----- SCROLL_TO_TOP end -----"); return; elseif eInnerPos.y >= 20 then callback( "SCROLL_TO_BOTTOM" ); print("----- SCROLL_TO_BOTTOM end -----"); return; end end callback( "NOT_DEFINE" ); end ); end
原文链接:https://www.f2er.com/cocos2dx/338549.html