cocos2dx-lua ScrollView的第一次编写

来源:互联网 发布:datamap 软件 编辑:程序博客网 时间:2024/06/01 09:26
--pType 0-水平、1-垂直
--pSpaceRow 行间距
--pSpaceCol 列间距
--pList     list
--pListView lsv
--pClass    Cell类别
--offBottom 偏移量
function getScrollContainerHorizontal(pType, pSpaceRow, pSpaceCol, pList, pListView, pClass, offBottom)
local callback = pList.callback
local from = pList.cellbasefrom --cell类型
local list = {}
for i=1, #pList do
table.insert(list, _G[pClass].new(pList[i], from, callback))
end
local cell = list[1]
local cellWight = cell.core:getContentSize().width + pSpaceCol--单位宽
local cellHeight = cell.core:getContentSize().height + pSpaceRow  --单位高
local listCount = #list        --list数量
local viewW = pListView:getContentSize().width             --可视宽度         
local viewH = pListView:getContentSize().height           --可视高度
local containerX = pListView:getPositionX()         --可视坐标X
local containerY = pListView:getPositionY()         --可视坐标Y

local numRow = math.floor(viewH / cellHeight)  --行可见数
local numCol = math.floor(viewW / cellWight)             --列可见数
local sumRow = math.ceil(listCount / numCol)               --内容总行数
local sumCol = math.ceil(listCount / numRow)--内容总列术


local contentW, contentH
if pType == 0 then
contentW = sumCol * cellWight
contentH = numRow * cellHeight
elseif pType == 1 then
contentW = numCol * cellWight                                          --内容宽度
contentH = sumRow * cellHeight                                         --内容高度
end
         
local layer = cc.LayerColor:create(cc.c4b(0x00, 0x00, 0x00, 0x00))          --内容层
layer:setContentSize(cc.size(contentW, contentH))                           --内容层大小
layer:ignoreAnchorPointForPosition(false)                      
layer:setAnchorPoint(cc.p(0, 1))                                            --设置锚点


-- 将每一个cell丢在layer上
if pType == 0 then                                                 --水平
for i=1, listCount do
local cellX = list[i]
cellX.core:setAnchorPoint(cc.p(0, 1))
cellX.core:setPosition(cc.p((i - 1) * cellWight, 0))
layer:addChild(cellX.core)
end
elseif pType == 1 then                                                --垂直
local index = 0
for i=1, sumRow do
for j=1, numCol do
index = index + 1
if index > listCount then break end
local cellX = list[index]
cellX.core:ignoreAnchorPointForPosition(false)
cellX.core:setAnchorPoint(cc.p(0, 1))               --更改锚点
layer:addChild(cellX.core)
cellX.core:setPosition(cc.p((j - 1) * cellWight, contentH - (i - 1) * cellHeight))  --设置每个cell的坐标(锚点是(0,1),so高度减)
end
end
end
if offBottom == nil then offBottom = 0 end
-- 滚动视图
local scrollView
if pType == 0 then
scrollView = CCScrollView:create(cc.size(viewW + offBottom, viewH), layer)   --创建滚动视图
scrollView:setDirection(kCCScrollViewDirectionHorizontal)                       
scrollView:setContentOffset(cc.p(0, 0), false)   --遮罩坐标,结合显示范围设置参数
elseif pType == 1 then
scrollView = CCScrollView:create(cc.size(viewW, viewH + offBottom), layer)   --创建滚动视图
scrollView:setDirection(kCCScrollViewDirectionVertical)
scrollView:setContentOffset(cc.p(0 , viewH + offBottom - contentH), false)
end
scrollView:ignoreAnchorPointForPosition(false)                               --更改图层锚点
scrollView:setAnchorPoint(cc.p(0, 1))
scrollView:setContainer(layer)                                                          --内容层添加到滚动视图上
scrollView:setContentSize(cc.size(contentW, contentH))--滚动视图大小(内容层大小)
scrollView:setPosition(containerX, containerY)       --滚动视图坐标

return scrollView, list
end