一.概述
本文缩写说明:sv = ScrollView, item代表ScrollView的一个子节点
如果对sv熟系程度还不够,请阅读基础篇:
Cocos2dx-lua ScrollView[一]基础篇-CSDN博客
本文介绍sv的一种封装类库,来实现快速创建sv,有如下几个优点:
1.item的位置通过参数控制,提高开发效率
2.免去了调用sv的API,提高开发效率
3.分帧创建,提高性能
4.可通过参数控制,复用item类似tableview,提高性能
二.效果演示
2.1 item不复用
2.2 item复用
三.代码实现
3.1 说明
a.下面2个模块需要require
b.svUtil是比较复杂的,有必要阅读代码掌握运行原理
c.item数量较多时才有必要用need_dynamic参数
d.代码原封不动搬到工程里基本可以正常运行(当然哪里出了问题读者得会排查,本文基本喂饭喂到嘴里了)
3.2 辅助定时器模块:GlobalTimeTicket
GlobalTimeTicket = GlobalTimeTicket or {}auto_id = auto_id or 0
function autoId()auto_id = auto_id + 1return auto_id
end
-- 获取单例
-- New和不New只是一层一层调用__init和__delete,对于单例没有影响
function GlobalTimeTicket:getInstance()if not self.is_init then self.scheduler = cc.Director:getInstance():getScheduler()self.schedulers = {}self.is_init = trueself.is_stop = nilendreturn self
end-- 定时回调 通用版
-- call_back : function 回调函数 必填
-- interval : int 时间间隔 默认1 秒
-- limit_time: int 限制次数 默认0 无限
-- with_name : any 定时器标识 默认自增id
-- 返回用于删除的标识
-- simple : local id = GlobalTimeTicket:getInstance():add(fun) ; GlobalTimeTicket:getInstance():remove(id)
-- : GlobalTimeTicket:getInstance():add(fun, 0.1, 1) -- 次数达到自动删除
-- : GlobalTimeTicket:getInstance():add(fun, 0.1, 3, "name")
function GlobalTimeTicket:add(call_back, interval, limit_time, with_name)if self.is_stop then return endwith_name = with_name or autoId()if nil == call_back or self.schedulers == nil or nil ~= self.schedulers[with_name] then return end -- 已经有定义了,不能重复limit_time = limit_time or 0interval = interval or 1local schedul_hander = self.scheduler:scheduleScriptFunc(function(dt)if self.is_stop then return endif call_back ~= nil thenif limit_time == 1 then self:remove(with_name)elseif limit_time > 1 then limit_time = limit_time - 1endcall_back(dt)endend, interval, false)self.schedulers[with_name] = schedul_handerreturn with_name
end-- 删除一个定时器
function GlobalTimeTicket:remove(with_name)if with_name == nil then return endlocal schedul_hander = self.schedulers[with_name] if schedul_hander ~= nil thenself.scheduler:unscheduleScriptEntry(schedul_hander)self.schedulers[with_name] = nil end
end-- 清除所有定时器
function GlobalTimeTicket:removeAll()for _, v in pairs(self.schedulers) do self.scheduler:unscheduleScriptEntry(v)endself.schedulers = {}
endfunction GlobalTimeTicket:hasTicket(with_name)local schedul_hander = self.schedulers[with_name] if schedul_hander ~= nil thenreturn trueendreturn false
endfunction GlobalTimeTicket:getSchedulers()return self.schedulers
end-- 停止定时器
function GlobalTimeTicket:stop()self.is_stop = trueself:removeAll()
end
3.3 sv封装模块:svUtil
----------------------------------------------------------------------------------- example -----------------------------------------------------------------------------------
--[[self.scr_lyt = AM.getNode(self.midPanel, "scr_lyt")local setting = {item_class = moonActivityPanelItem,start_x = 24.5, space_x = 0,start_y = 5, space_y = 10,item_width = 621, item_height = 126,row = 0, col = 1,--need_dynamic = true}self.item_scrollview = svUtil.new(self.scr_lyt, cc.p(0,0) , ScrollViewDir.vertical, ScrollViewStartPos.top, self.scr_lyt:getContentSize(), setting)local svData = {}self.item_scrollview:setData(svData)
]]
----------------------------------------------------------------------------------- example -----------------------------------------------------------------------------------
display.DEFAULT_FPS = 60
ScrollViewDir = ScrollViewDir or {vertical = 1,horizontal = 2,
}ScrollViewStartPos = ScrollViewStartPos or {top = 1,bottom = 2
}function tableLen(table)local len = 0if table thenfor _ in pairs(table) dolen = len + 1endendreturn len
endfunction createScrollView(width,height,x,y,parent_wnd,type)local scroll_view = ccui.ScrollView:create()scroll_view:setBounceEnabled(true)scroll_view:setScrollBarEnabled(false)if type == ccui.ScrollViewDir.horizontal thenscroll_view:setDirection(ccui.ScrollViewDir.horizontal)elsescroll_view:setDirection(ccui.ScrollViewDir.vertical)endscroll_view:setTouchEnabled(true)scroll_view:setContentSize(cc.size(width, height))scroll_view:setInnerContainerSize(scroll_view:getContentSize())scroll_view:setPosition(cc.p(x, y))if not tolua.isnull(parent_wnd) thenparent_wnd:addChild(scroll_view)endreturn scroll_view
endsvUtil = class("svUtil", function()return ccui.Layout:create()
end)function svUtil:ctor(parent, pos, dir, start_pos, size, setting, ap