Advertisement
reshetnikoff

Untitled

Jul 18th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.48 KB | None | 0 0
  1. -----------------------------------------------------------------------------------------
  2. --
  3. -- main.lua
  4. --
  5. -----------------------------------------------------------------------------------------
  6.  
  7. -- Your code here
  8.  
  9. local composer = require("composer");
  10. local widget = require("widget");
  11. local json = require("json");
  12.  
  13. composer.removeScene('scenes.menu')
  14.  
  15. local scene = composer.newScene();
  16.  
  17. function scene:show(event)
  18.     local sceneGroup = self.view;
  19.  
  20.     -- создаём группу для ввода веса
  21.     local contentGroup = display.newGroup();
  22.  
  23.  
  24.     local function scrollListener(event)
  25.         local phase = event.phase
  26.         local direction = event.direction
  27.  
  28.         -- если долистает до конца - дойдет до лимита
  29.         if event.limitReached then
  30.             if "up" == direction then
  31.                 print("Reached Top Limit")
  32.             elseif "down" == direction then
  33.                 print("Reached Bottom Limit")
  34.             end
  35.         end
  36.  
  37.         return true
  38.     end
  39.  
  40.     -- создаем скролвью
  41.     local scrollView = widget.newScrollView
  42.         {
  43.             left = 0,
  44.             top = 0,
  45.             width = display.contentWidth,
  46.             height = display.contentHeight,
  47.             topPadding = 50,
  48.             bottomPadding = 50,
  49.             horizontalScrollDisabled = true,
  50.             verticalScrollDisabled = false,
  51.             listener = scrollListener,
  52.         }
  53.  
  54.  
  55.     local contentGroup = display.newGroup()
  56.     -- рисую первую карточку
  57.     local backgrRect = display.newRect(contentGroup, 0, 5, display.contentWidth, 110 ):setFillColor(0.4)
  58.  
  59.     local textTitle = "dfdsf"
  60.     local textTitleObject = display.newText(contentGroup, textTitle, 110, -20, 300, 50, "Helvetica", 20)
  61.  
  62.     local textDescr = "dfdsf sf df we fwef wefwefwef wef we fwe we wef we we few we we wef wef wef we fwe fwe fwe fwej wjef wej fiwoej fiowej fiwej fowiej foiwje fiowje foiwjef oiwej iofwej fiowje iofjweio fjweio fjwoiej fwioej owiejf oiwej oiwej fiowejf iowejf iofewj fiowjefo ifjweio jfweio jiowej iowefj fiowej oifwejf oifwejio ffjweofi jfweo jwoej eoiwjf oiwje iowjeo ifjwefio jfweoi jewoifj fwoefjf wio"
  63.     local textDecsrObject = display.newText(contentGroup, textDescr, 60, 25, 200, 50, "Helvetica", 14)
  64.  
  65.     local imageContent = display.newImageRect(contentGroup, "img/img1.jpg", 100, 100); 
  66.     imageContent.x = -display.contentWidth/3
  67.     imageContent.y = 5
  68.  
  69.     -- lotsOfTextObject:setTextColor(0)
  70.     contentGroup.x = display.contentCenterX
  71.     scrollView:insert(contentGroup)
  72.  
  73.  
  74.  
  75.  
  76.  
  77.     local function coroutineDraw()
  78.      
  79.             --логика создания карточки тут
  80.      
  81.         for i = 1,20 do
  82.  
  83.             local contentGroup = display.newGroup()
  84.            
  85.             local backgrRect = display.newRect(contentGroup, 0, 5, display.contentWidth, 110 ):setFillColor(0.4)
  86.  
  87.             local textTitle = "dfdsf"
  88.             local textTitleObject = display.newText(contentGroup, textTitle, 110, -20, 300, 50, "Helvetica", 20)
  89.  
  90.             local textDescr = "dfdsf sf df we fwef wefwefwef wef we fwe we wef we we few we we wef wef wef we fwe fwe fwe fwej wjef wej fiwoej fiowej fiwej fowiej foiwje fiowje foiwjef oiwej iofwej fiowje iofjweio fjweio fjwoiej fwioej owiejf oiwej oiwej fiowejf iowejf iofewj fiowjefo ifjweio jfweio jiowej iowefj fiowej oifwejf oifwejio ffjweofi jfweo jwoej eoiwjf oiwje iowjeo ifjwefio jfweoi jewoifj fwoefjf wio"
  91.             local textDecsrObject = display.newText(contentGroup, textDescr, 60, 25, 200, 50, "Helvetica", 14)
  92.  
  93.             local imageContent = display.newImageRect(contentGroup, "img/img1.jpg", 100, 100); 
  94.             imageContent.x = -display.contentWidth/3
  95.             imageContent.y = 5
  96.  
  97.             -- lotsOfTextObject:setTextColor(0)
  98.             contentGroup.x = display.contentCenterX
  99.             contentGroup.y = (110+10)*i
  100.             scrollView:insert(contentGroup)
  101.  
  102.  
  103.  
  104.             coroutine.yield()
  105.         end
  106.     end
  107.  
  108.     local loadingCoroutineData
  109.     loadingCoroutineData = {
  110.         clear = function()
  111.             if (loadingCoroutineData.co == nil) then return end
  112.             print("loadingCoroutineData clear")
  113.             Runtime:removeEventListener("enterFrame", loadingCoroutineData)
  114.             loadingCoroutineData.co = nil
  115.             loadingCoroutineData.enterFrame = nil
  116.         end
  117.     }
  118.  
  119.  
  120.  
  121.     local function renderSomething()
  122.  
  123.         loadingCoroutineData.clear()
  124.         loadingCoroutineData.co = coroutine.create(coroutineDraw)
  125.  
  126.        --захардкоженное значение: лучше вернуть возвращать из coroutine.yield координаты
  127.        --нижней точки текущей карточки, чтобы определить что мы заполнили видимую часть скрола
  128.        for i = 1, 5 do
  129.             local res, err = coroutine.resume(loadingCoroutineData.co)
  130.                 if (not res) then
  131.                     print("coroutine.resume", res, err)
  132.                     loadingCoroutineData.co = nil
  133.                 return
  134.             end
  135.         end
  136.  
  137.        --продолжаем рисовать каждый кадр
  138.     loadingCoroutineData.enterFrame = function()
  139.            print("====loadingCoroutineData====")
  140.                 local t = os.clock()
  141.                 local res, err
  142.                 repeat
  143.                 res, err = coroutine.resume(loadingCoroutineData.co)
  144.                 print("resume ", res, os.clock() - t)
  145.             until (not res or (os.clock() - t) > 0.015) --тут 15ms задержки, для плавности можно уменьшить это число
  146.      
  147.             if (not res) then
  148.                 print("coroutine ended", res, err)
  149.                 loadingCoroutineData.clear()
  150.             end
  151.     end
  152.  
  153.     Runtime:addEventListener("enterFrame", loadingCoroutineData)
  154.  
  155.  
  156. end
  157.  
  158. end
  159.  
  160. scene:addEventListener("show", scene)
  161. return scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement