lua-使用滚动容器ScrollView

来源:互联网 发布:纯t是什么意思网络用语 编辑:程序博客网 时间:2024/05/11 15:05

ScrollView是cocos2dx ui里面封装的一个控件,首先看下C++中创建ScrollView

using namespace cocos2d::ui;//首先要使用ui命名空间ui::ScrollView* scrollView = ui::ScrollView::create();scrollView->setContentSize(Size(280.0f, 100.0f));scrollView->setScrollBarWidth(4);scrollView->setScrollBarPositionFromCorner(Vec2(2, 2));scrollView->setScrollBarColor(Color3B::WHITE);scrollView->setGlobalZOrder(200);_uiLayer->addChild(scrollView);

再看下用lua创建ScrollView(这里先用cocosstudio编辑一个滚动控件)

这里写图片描述
这里写图片描述
一般设置控件尺寸和滚动区域大小一致,方便整个区域滑动

local gameListScrollView = self.studio.layout:getChildByName("ScrollView_GameList")--测试(动态加载游戏列表)gameListScrollView:setVisible(true)gameListScrollView:setScrollBarEnabled(false);for i=1,GameListManager_getInstance():getCount() do--向滚动容器添加控件    local _gameInfo=GameListManager_getInstance():getGameByIndex(i)    local _gameImageFile = string.format("lobby/LobbyScene/%d.png",_gameInfo.uNameID)    local _textureCache = cc.Director:getInstance():getTextureCache()          local _Texture = _textureCache:addImage(_gameImageFile)      if _Texture then        self.gameButton[i]=ccui.Button:create(_gameImageFile);    else        self.gameButton[i]=ccui.Button:create("lobby/LobbyScene/default.png");        self.gameButton[i]:setTitleText(_gameInfo.szGameName);    end    self.gameButton[i]:setTag(_gameInfo.uNameID);    self.gameButton[i]:setAnchorPoint(cc.p(0.5, 0.5))    self.gameButton[i]:setPosition(cc.p(194+433*(i-1),249));    self.gameButton[i]:addClickEventListener(gameBtnClick)    gameListScrollView:addChild(self.gameButton[i]);end--scroll:setContentSize(cc.size(433*13,500));--这里设置控件内容大小,编辑器里设置过了就不用设置了--scroll:setViewSize()此组函数在lua中不起作用--scroll:getViewSize()gameListScrollView:setInnerContainerSize(cc.size(433*GameListManager_getInstance():getCount(),500));--设置ScrollView容器大小,随着ScrollView容纳的控件越多,此值的设置值应该也变大--这里只是做个打印local scrol_content=gameListScrollView:getContentSize();print("lwlog::scrol_content.width="..scrol_content.width.." scrol_content.height="..scrol_content.height)local scrol_inner=gameListScrollView:getInnerContainerSize();print("lwlog::scrol_inner.width="..scrol_inner.width.." scrol_inner.height="..scrol_inner.height)

我自己将公司手游大厅游戏列表改成滚动,效果图如下
这里写图片描述
ScrollView控件暂时就了解这么多,希望以后工作中对ScrollView能有更深层次的了解

原创粉丝点击