I create scrolling for the menu in the Lua CORONA SDK.

Source file menu.lua

local composer = require( "composer" ) local widget = require( "widget" ) local scene = composer.newScene() local function onButtonRelease( event ) composer.gotoScene( event.target.id:lower(), { effect="fade", time=300 } ) --composer.recycleOnSceneChange = true end function scene:create( event ) local sceneGroup = self.view local sceneTitle = display.newText( sceneGroup, "Выберете уровень", display.contentCenterX, 10, composer.getVariable( "appFont" ), 20 ) -- Создание массива из кнопок меню local menuButtons = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18" } -- Создание цикла для кнопок меню local rowNum = 0 for i = 1,#menuButtons do rowNum = rowNum+1 local button = widget.newButton( { label = menuButtons[i], id = menuButtons[i], shape = "circle", radius = 20, font = composer.getVariable( "appFont" ), fontSize = 16, fillColor = { default={ 0.12,0.32,0.52,1 } ,over={ 0.132,0.352,0.572,1 } }, -- цвет кнопки и нажатия на кнопку labelColor = { default={ 1,1,1,1 }, over={ 1,1,1,1 } }, -- цвет шрифта на кнопках onRelease = onButtonRelease }) mod = math.fmod(i, 2) -- определение кратности if (i*mod >= 1 ) then -- если кратно 1 button.x = display.contentCenterX -50 elseif (i*mod == 0) then -- если кратно 0 button.x = display.contentCenterX + 50 end button.y = 65 + ((rowNum-1)*35) -- растояние по y между кнопками sceneGroup:insert( button ) -- обновление сцены после нажатия на кнопку end end scene:addEventListener( "create", scene ) -- слушатель на создание сцены --обработка касаний function scene:touch(e) -- body if(e.phase == "began") then print("начало a"); elseif (e.phase == "moved") then scene.x = ex; scene.y = ey; print("двигаю a"); elseif(e.phase == "ended") then print("отпустил a"); end end scene:addEventListener("touch", scene); return scene 

For clarity, attach the image

For clarity, attach the image

For touch processing, I tried both with the scene, and with the menu, and with an array of buttons.

Tell me, please, the solution to my problem.

    1 answer 1

    To accomplish this goal, you need to create a scrollView and move the code in the function to create buttons in the loop from function scene:create to function showSlidingMenu and listen to events every time during Runtime. To implement the scrolling of the menu immediately upon going to the menu, the function is called in scene:create

     local composer = require( "composer" ) local widget = require( "widget" ) local scrollView local scene = composer.newScene() local function onButtonRelease( event ) composer.gotoScene( event.target.id:lower(), { effect="fade", time=300 } ) end function scene:create( event ) sceneGroup = self.view composer.recycleOnSceneChange = true; local sceneTitle = display.newText( sceneGroup, "Выберете уровень", display.contentCenterX, 10, composer.getVariable( "appFont" ), 20 ) end local function ButtonListener( event ) if ( event.phase == "moved" ) then local dx = math.abs( event.x - event.xStart ) if ( dx > 5 ) then scrollView:takeFocus( event ) end elseif ( event.phase == "ended" ) then print( "клик по объекту" ) timer.performWithDelay( 10, function() scrollView:removeSelf(); scrollView = nil; Runtime:removeEventListener("touch",showSlidingMenu) end ) end return true end function showSlidingMenu( event ) if ( "ended" == event.phase ) then scrollView = widget.newScrollView { width = _W, height = _H, scrollWidth = width, scrollHeight = height, horizontalScrollDisabled = true } scrollView.x = display.contentCenterX scrollView.y = display.contentCenterY local menuButtons = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18" } rowNum = 0 for i = 1,#menuButtons do rowNum = rowNum+1 local button = widget.newButton( { label = menuButtons[i], id = menuButtons[i], shape = "circle", radius = 20, font = composer.getVariable( "appFont" ), fontSize = 16, fillColor = { default={ 0.12,0.32,0.52,1 } ,over={ 0.132,0.352,0.572,1 } }, -- цвет кнопки и нажатия на кнопку labelColor = { default={ 1,1,1,1 }, over={ 1,1,1,1 } }, -- цвет шрифта на кнопках onRelease = onButtonRelease }) mod = math.fmod(i, 2) -- определение кратности if (i*mod >= 1 ) then -- если кратно 1 button.x = display.contentCenterX -50 -- смещение относительно центра влево elseif (i*mod == 0) then -- если кратно 0 button.x = display.contentCenterX + 50 -- смещение относительно центра впрово end button.y = 30 + ((rowNum-1)*35) -- координаты первой кнопки и растояние по y между кнопками scrollView:insert( button ) sceneGroup:insert( scrollView ) scene:addEventListener( "touch", ButtonListener ) end return true end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Код здесь работает, когда сцена находится на экране (но собирается уйти с экрана) print ("ухожу с экрана"); elseif ( phase == "did" ) then -- Код здесь запускается сразу после того, как сцена полностью выходит за пределы экрана print ("ушел за пределы экрана"); end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view showSlidingMenu = nil; end scene:addEventListener( "create", scene ) -- слушатель на создание сцены scene:addEventListener( "destroy", scene ) scene:addEventListener( "hide", scene ) Runtime:addEventListener("touch",showSlidingMenu) return scene 

    If the menu creation theme using the Corona SDK is interesting, I will develop it