Lua Corona - 如何在滑动过程中聆听 widget.newScrollView 监听器
Lua Corona - how to listen to widget.newScrollView listener during sliding
我的 widget.newScrollView 组件后面有一张背景图片。
我想以我的 newScrollView 一半的速度移动我的背景图像。
当我拖动我的滚动视图时这工作正常但是当我释放鼠标时滚动视图保持滑动但我的背景停止移动。请告诉我如何捕获滑动事件。
非常感谢
这是我的一些代码:
local function scrollListener( event )
local phase = event.phase
if ( phase == "began" ) then print( "Scroll view was touched" )
elseif ( phase == "moved" ) then print( "Scroll view was moved" )
nebulaeBgnd.x = event.x/2;
elseif ( phase == "ended" ) then print( "Scroll view was released" )
end
end
local a1 = display.newImage( "planetHexs/001.png", _topLeft_x, _topLeft_y);
local scrollView = widget.newScrollView(
{
top = 0,
left = 0,
width = display.actualContentWidth,
height = display.actualContentHeight,
scrollWidth = 0,
scrollHeight = 0,
listener = scrollListener,
backgroundColor = { 0, 0, 0},
verticalScrollDisabled=true;
}
scrollView:insert( a1 )
您可以使用运行时 enterFrame
侦听器执行此操作。我们有时会在游戏中这样做,而且效果很好。例如:
Runtime:addEventListener("enterFrame", function()
local scrollX, scrollY = scrollView:getContentPosition()
nebulaeBgnd.x = scrollX * 0.5 -- Tip: multiplication is faster than division
end)
无论是否被触摸,它都会不断地监听你的滚动视图位置。您可能需要通过增加或减少屏幕宽度的一半来补偿 x,具体取决于您的滚动视图位置设置。您可以阅读有关 getContentPosition here.
关于运行时事件侦听器的非常重要注意事项:
Runtime event listeners, on the other hand, must be removed when you're finished using them. Otherwise, they'll continue to run because the Runtime event is global. Not only will this cause a memory leak, but if any functions executing in the Runtime attempt to reference object(s) that no longer exist, the program will crash. See Removing Event Listeners below for more information.
我的 widget.newScrollView 组件后面有一张背景图片。 我想以我的 newScrollView 一半的速度移动我的背景图像。 当我拖动我的滚动视图时这工作正常但是当我释放鼠标时滚动视图保持滑动但我的背景停止移动。请告诉我如何捕获滑动事件。 非常感谢
这是我的一些代码:
local function scrollListener( event )
local phase = event.phase
if ( phase == "began" ) then print( "Scroll view was touched" )
elseif ( phase == "moved" ) then print( "Scroll view was moved" )
nebulaeBgnd.x = event.x/2;
elseif ( phase == "ended" ) then print( "Scroll view was released" )
end
end
local a1 = display.newImage( "planetHexs/001.png", _topLeft_x, _topLeft_y);
local scrollView = widget.newScrollView(
{
top = 0,
left = 0,
width = display.actualContentWidth,
height = display.actualContentHeight,
scrollWidth = 0,
scrollHeight = 0,
listener = scrollListener,
backgroundColor = { 0, 0, 0},
verticalScrollDisabled=true;
}
scrollView:insert( a1 )
您可以使用运行时 enterFrame
侦听器执行此操作。我们有时会在游戏中这样做,而且效果很好。例如:
Runtime:addEventListener("enterFrame", function()
local scrollX, scrollY = scrollView:getContentPosition()
nebulaeBgnd.x = scrollX * 0.5 -- Tip: multiplication is faster than division
end)
无论是否被触摸,它都会不断地监听你的滚动视图位置。您可能需要通过增加或减少屏幕宽度的一半来补偿 x,具体取决于您的滚动视图位置设置。您可以阅读有关 getContentPosition here.
关于运行时事件侦听器的非常重要注意事项:
Runtime event listeners, on the other hand, must be removed when you're finished using them. Otherwise, they'll continue to run because the Runtime event is global. Not only will this cause a memory leak, but if any functions executing in the Runtime attempt to reference object(s) that no longer exist, the program will crash. See Removing Event Listeners below for more information.