首先添加响应事件:
self:addTouchEventListener(self.m_scroll, onScrollEvent)
在ScrollView移动的时候调用onScrollViewChangeMsg函数
local function onScrollEvent(l_u, l_evntype, self)
self.m_currEventType = l_evntype
if l_evntype == 1 then
self:onScrollViewChangeMsg()
end
end
用一个m_percent 变量记录相应的竖向移动百分比:
function onScrollViewChangeMsg(self)
if self.m_scroll == nil then return end
if self.m_fnScrollStop ~= nil then return end
--update函数内部变量
local l_prevFrameTick = nil --前一次调用的时间
local l_currScrollY = nil --当前滚动内容的Y坐标
local l_rh = nil
local l_y = nil
self.m_fnScrollStop = delaycall_loop(0,
function ()
if self.m_scroll == nil then return end
l_y = self.m_scroll:getInnerContainer():getPositionY()
if l_currScrollY ~= l_y then
l_currScrollY = l_y
l_rh = self.m_scroll:getInnerContainerSize().height - self.m_scroll:getContentSize().height
self.m_percent = (l_rh + l_y)/l_rh * 100
return
end
if(self.m_currEventType == 0 or self.m_currEventType == 1) then return end
if(l_prevFrameTick == nil) then
l_prevFrameTick = getTimer()
elseif (getTimer() - l_prevFrameTick) >= 1000 then
self.m_fnScrollStop.Stop()
self.m_fnScrollStop = nil
end
end
)
end