我有一個滾動按鈕時,scrollView的問題。問題在於事件處理不當。所有試圖將焦點傳遞給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)
端
- - 巴頓事件H andler
本地函數handleButtonEvent(事件)
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
末
不工作,仍然有像以前一樣的問題,也不會只發送階段,而不是整個事件? – MasterNoda 2014-11-21 17:06:28