电晕按钮没有将焦点传递给scrollView

时间:2014-11-20 22:44:09

标签: button lua widget scrollview corona

当滚动视图上的按钮滚动时,我遇到了滚动问题。问题是没有正确处理事件的问题。将焦点传递给scrollView的所有尝试都证明是徒劳的。我观察到的是,在下面的代码中,它不会打印回来,即使在我没有按钮的情况下正确滚动之后也是如此。

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" )
elseif ( phase == "ended" ) then print( "Scroll view was released" )
end

-- In the event a scroll limit is reached...
if ( event.limitReached ) then
    if ( event.direction == "up" ) then print( "Reached top limit" )
    elseif ( event.direction == "down" ) then print( "Reached bottom limit" )
    elseif ( event.direction == "left" ) then print( "Reached left limit" )
    elseif ( event.direction == "right" ) then print( "Reached right limit" )
    end
end

return true

以下是用于场景的实际代码:

功能场景:enterScene(事件)

--local yell=audio.play(ahhh, {channel=1, loops=0, fadein=0})

--Creating the Background
local background=display.newImageRect("background.png", 320, 580)
background.x=160
background.y=240


--Creating the scroll view
local scrollView = widget.newScrollView
{
    top = 10,
    left = 10,
    width = 300,
    height = 500,
    scrollWidth = 300,
    scrollHeight = 500,
    horizontalScrollDisabled = true,
    listener = scrollListener
}

local button = {}


local yCount = 0


for i = 1 , 11 do
    button[i] = widget.newButton{
        label = "Button " .. i,
        left = 0,
        id = button[i],
        top = yCount,
        width = 300,
        height = 100,
        defaultFile = "menuButton.png",
        onEvent = handleButtonEvent
    }

    yCount = yCount + 100
    scrollView:insert(button[i])

end



--local background=display.newRect(160, 270, 320, 510)
--scrollView:insert( background )

local menu=display.newImageRect("menu2.png", 90, 50)
menu.x=50
menu.y=-15



function menu:touch(event)
    if event.phase=="began" then
        --local illBeBack=audio.play(terminator,{channel=1, loops=0, fadein=0})
        display.remove(menu)
        display.remove(taskBar)
        display.remove(background)

        taskBar = nil
        background = nil
        menu = nil
        storyboard.gotoScene("menu")
    end
    return true
end
menu:addEventListener("touch", menu)

- 按钮事件处理程序

本地函数handleButtonEvent(event)

local phase = event.phase

if ( phase == "moved" ) then
    local dy = math.abs( ( event.y - event.yStart ) )
    -- If the touch on the button has moved more than 10 pixels,
    -- pass focus back to the scroll view so it can continue scrolling
    if ( dy > 10 ) then
        scrollView:takeFocus( event )
    end
end
return true

2 个答案:

答案 0 :(得分:0)

尝试将event.phase传递给scrollView,如下所示:

scrollView:takeFocus(event.phase)

答案 1 :(得分:0)

通过删除本地'解决了这个问题。来自' handleButtonEvent'和' scrollListener'功能,以及删除本地'来自' scrollView'插件

所以看来,在日冕,或者至少是我正在制作的程序中,除非有其他人有解决方案,否则全局变量必须比它们应该使用的多得多。