https://img-blog.csdnimg.cn/32db9ce43ef64316a2e37a31f4cee033.gif
编程小鱼酱yu612.com,点击前往

cocos2d-lua3.7控件篇(四)-ScrollView demo


效果如下:


local MenuScene = class("MenuScene", function()
    return display.newScene("MenuScene")
end)

function MenuScene:ctor()
    print("ctor")
local tests = {
        "Test_NodeFrameEvent",
        "Test_NodeEvent",
        "Test_KeypadEvent",
        "Test_NodeTouchEvent",
        "Test_AccelerometerEvent",
        "Test_CocosStudio",
        "Test_Audio",
    }

    local scrollView = ccui.ScrollView:create()
    scrollView:addTo(self)
    scrollView:align(display.TOP_CENTER, display.cx, display.top)
    self.scrollView = scrollView

    local total = 0
    local btnSize = nil
    for i = #tests, 1, -1 do
        local btn = ccui.Button:create()
        btn:setTitleText(tests[i])
        btn:setTitleFontSize(24)
        btn:addTouchEventListener(function(sender, eventType)
            if 2 == eventType then
                print(i)
                
            end
        end)
        if not btnSize then
            btnSize = btn:getContentSize()
        end
        btn:pos((display.width - btnSize.width) / 2 + btnSize.width / 2,
                btnSize.height * total + btnSize.height / 2)
        total = total + 1

        scrollView:addChild(btn)
    end

    local totalHeight = btnSize.height * total
    scrollView:setInnerContainerSize(cc.size(display.width, totalHeight))
    local scrollHeight = display.height
    if totalHeight < scrollHeight then
        scrollHeight = totalHeight
    end
    scrollView:setContentSize(cc.size(display.width, scrollHeight))

end

function MenuScene:onEnter()
end

function MenuScene:onExit()
end

return MenuScene


posted @ 2017-12-14 09:17  鱼酱  阅读(475)  评论(0编辑  收藏  举报

https://img-blog.csdnimg.cn/32db9ce43ef64316a2e37a31f4cee033.gif
编程小鱼酱yu612.com,点击前往