我前面有一个widget.newScrollView组件和一个widget.newButton。不幸的是,当我点击我的按钮时,它也会调用我的ScrollView“点击”处理程序。我如何阻止我的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
发布于 2016-10-02 03:52:16
必须对事件函数返回true
以防止传播。这从本质上告诉Corona,事件被正确地处理,并且不应该激发更多的事件侦听器。您可以在文档中阅读有关事件传播的更多信息。。
"tap"
和"touch"
事件是由不同的侦听器处理的,因此,如果您希望在单击按钮时停止点击,那么您也可以向按钮中添加一个"tap"
侦听器,这实际上只是返回true
来防止,或者阻止点击事件到按钮后面的任何东西。
button1:addEventListener("tap", function() return true end)
因为按钮没有tap事件,所以tap事件只需经过按钮就可以到达它后面的任何有"tap"
事件的对象。
https://stackoverflow.com/questions/39806579
复制相似问题