lua corona - 如何禁用触摸事件 widget.newScrollView

lua corona - how to disable touch events widget.newScrollView

我有一个 widget.newScrollView 组件和一个 widget.newButton 在它前面。不幸的是,当我单击我的按钮时,它还会调用我的 ScrollView "tap" 处理程序。如何阻止我的 ScrollView 获取此事件? 这是我正在使用的一些代码:

local function handleButtonEvent( event )
    if ( "ended" == event.phase ) then
        print( "Button was pressed and released" )
    end
    return true; **I tried this - but it had no effect**
end

已添加

local button1 = widget.newButton(
{
    label = "button",
    onEvent = handleButtonEvent,
    emboss = false,
    shape = "roundedRect",
    width = 400,
    height = 100,
    cornerRadius = 32,
    fillColor = { default={1,0,0,1}, over={1,0.1,0.7,1} },
    strokeColor = { default={1,0.4,0,1}, over={0.8,0.8,1,1} },
    strokeWidth = 4,
    fontSize=100;
}

我有一个 display.NewImages 数组(行星)和我的处理程序 - 像这样:

local planets = {};
planets[1] = display.newImage( "planetHexs/001.png", _topLeft_x, _topLeft_y);
planets[2] = display.newImage( "planetHexs/002.png", _topLeft_x, _topLeft_y + _planet_height2 );
....

local scrollView = widget.newScrollView(
{
    top = 0,
    left = 0,
    width = display.actualContentWidth,
    height = display.actualContentHeight,
    scrollWidth = 0,
    scrollHeight = 0,
    backgroundColor = { 0, 0, 0, 0.5},
    verticalScrollDisabled=true;
}

for i = 1, #planets do
    local k = planets[i];
    scrollView:insert( k )
end

function PlanetTapped( num )
    print( "You touched the object!"..num );
end

for i = 1, #planets do
    local k = planets[i];
    k:addEventListener( "tap", function() PlanetTapped(i) end )
end

我得到这个打印日志:

Button was pressed and released

You touched the object2

您必须 return true 您的事件函数以防止传播。这实质上告诉 Corona 事件已正确处理,并且不应触发该事件的更多事件侦听器。 You can read more about event propagation here in the docs

"tap""touch" 事件由不同的侦听器处理,因此如果您希望在触摸按钮时停止点击,您需要向按钮添加一个 "tap" 侦听器同样,这基本上只是 returns true 来防止或阻止对其背后的任何点击事件。

button1:addEventListener("tap", function() return true end)

由于按钮没有点击事件,点击事件只需通过按钮,到达按钮后面确实有"tap"事件的任何对象。