cocos2dx-lua ScrollView的第一次编写

--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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值