If you want buttons inside a scrollview without using the Widget library, you can use the function
“display.getCurrentStage():setFocus()”
in order to shift the touch focus from the button to the scrollview as soon as the user moves his finger more than 5 pixels.
This means that further touch events will go to the scrollview, until the user lifts his finger.
[lua]-- This assumes that multitouch is not activated.
– (If multitouch is activated, the setFocus() function needs more parameters.)
– create your own scrollView
– local scrollView = …
– create your own button
– local button = …
– put a reference to scrollView into the button table
– button.scrollView = scrollView
local function onTouchButton(event)
if event.phase == “began” then
display.getCurrentStage():setFocus(event.target)
event.target.isFocus = true
elseif event.phase == “moved”
and event.target.isFocus then
– Before, we have put the property ‘scrollView’ into the button.
– It means that this button is inside a scrollView, and the property
– is a reference to the scrollview.
– When the user moves his finger more than 5 pixels, we shift the
– touch focus from the button to the scrollview.
if event.target.scrollView
and (math.abs(event.x - event.xStart) > 5
or math.abs(event.y - event.yStart) > 5) then
event.target.isFocus = false
display.getCurrentStage():setFocus(event.target.scrollView)
scrollView.isFocus = true
return true
end
elseif (event.phase == “ended”
or event.phase == “cancelled”)
and event.target.isFocus then
display.getCurrentStage():setFocus(nil)
event.target.isFocus = false
print(“touch ended”)
end
return true
end
local function onTouchScrollView(event)
if event.phase == “began” then
display.getCurrentStage():setFocus(event.target)
event.target.isFocus = true
elseif event.phase == “moved”
and event.target.isFocus then
– move scrollView
elseif (event.phase == “ended”
or event.phase == “cancelled”)
and event.target.isFocus then
display.getCurrentStage():setFocus(nil)
event.target.isFocus = false
end
return true
end
button:addEventListener(“touch”, onTouchButton)
scrollView:addEventListener(“touch”, onTouchScrollView)[/lua] [import]uid: 146894 topic_id: 28846 reply_id: 116981[/import]