我想在滚动视图中用滚动动作移动我的"背景"图像,我希望背景移动得比滚动视图的内容慢,以给人深度的印象。这是我正在使用的代码。它可以工作,但前提是屏幕正在接收触摸。我希望它继续以滚动的势头移动。我做错了什么?
local function scrollListener( event )
local phase = event.phase
local x, y = scrollView:getContentPosition()
if phase == "moved" then
if event.limitReached then
-- do nothing
else
bg.x = x /3
end
end
return true
end
我认为您想要实现一些视差滚动。您应该从滚动视图scrollListener(事件)"移动"事件获得拖动距离
local isListening = false
local function scrollListener( event )
local phase = event.phase
if "began" == phase then
if(isListening) then
Runtime:addEventListener( "enterFrame", updateBgPos )
isListening = true
end
elseif phase == "moved" then
if event.limitReached then
Runtime:removeEventListener( updateBgPos )
isListening = false
end
end
return true
end
local function updateBgPos(event)
local x, y = scrollView:getContentPosition()
bg.x = x / 3
end