lua corona - 如何禁用 touch 事件 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; **我试过这个,但它没有效果**
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;
}

我有一个显示图片的数组 (planets),以及像这样的处理程序:

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
点赞
用户1381216
用户1381216

你必须在事件函数中返回 true,以防止事件冒泡。这基本上告诉 Corona 事件已经被正确处理,没有更多的事件监听器应该被触发。你可以在这里阅读有关事件传播的更多文档

"tap""touch" 事件是通过不同的监听器处理的,因此如果你希望在触摸按钮时停止点击,你必须添加一个 "tap" 监听器到你的按钮中,然后只需返回 true 以防止或阻止点击事件传递到其背后的任何对象。

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

因为按钮没有 tap 事件,点击事件会直接穿过按钮传递到任何有 "tap" 事件的对象后面。

2016-10-02 03:52:16