I’m wanting to use the scrollview widget as a level selection tool, and want the nearest scrollview child to be transitioned/snapped to center X after the touch event ends. Unfortunately all scrollview children.x = 0, so I am unsure how to proceed which child needs to be targeted, and how to move them, and the rest of the scrollview objects to the correction x-coordinate.
I know that object:scrollToPosition() may be a path to go down, but I’m not seeing how to make it dynamically work with the scrollview objects.
Below is my test that all children.x = 0, no matter how much you move them around.
Thank you for any input.
[lua]
local widget = require “widget”
local mainGroup = display.newGroup( )
local scrollView
local function scrollListener( event )
target = event.target – this is the scrollView
print("scrollView[3].x = "…scrollView[1].x)
end
scrollView = widget.newScrollView{
width = display.contentWidth,
height = 250,
x = display.contentWidth * 0.5,
y = display.contentHeight * 0.5,
backgroundColor = {1,1,1},
listener = scrollListener
}
for i=1, 4 do
local box = display.newRect( 0, 0, 300, 200 )
box:setFillColor(0.2 * i, 0.2 * i, 0.2)
box.anchorX, box.anchorY = 0,0.5
box.x = (i -1 ) * box.contentWidth
box.y = scrollView.contentHeight * 0.5
scrollView:insert(box)
end
[/lua]