本文缩写说明:sv = ScrollView, item代表ScrollView的一个子节点
如果对sv熟系程度还不够,请阅读基础篇:
Cocos2dx-lua ScrollView[一]基础篇-CSDN博客
本文介绍sv的一种封装类库,来实现快速创建sv,有如下几个优点:
1.item的位置通过参数控制,提高开发效率
2.免去了调用sv的API,提高开发效率
3.分帧创建,提高性能
4.可通过参数控制,复用item类似tableview,提高性能
a.下面2个模块需要require
b.svUtil是比较复杂的,有必要阅读代码掌握运行原理
c.item数量较多时才有必要用need_dynamic参数
d.代码原封不动搬到工程里基本可以正常运行(当然哪里出了问题读者得会排查,本文基本喂饭喂到嘴里了)
- GlobalTimeTicket = GlobalTimeTicket or {}
-
- auto_id = auto_id or 0
- function autoId()
- auto_id = auto_id + 1
- return 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 = true
- self.is_stop = nil
- end
- return 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 end
- with_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 0
- interval = interval or 1
- local schedul_hander = self.scheduler:scheduleScriptFunc(function(dt)
- if self.is_stop then return end
- if call_back ~= nil then
- if limit_time == 1 then
- self:remove(with_name)
- elseif limit_time > 1 then
- limit_time = limit_time - 1
- end
- call_back(dt)
- end
- end, interval, false)
- self.schedulers[with_name] = schedul_hander
- return with_name
- end
-
- -- 删除一个定时器
- function GlobalTimeTicket:remove(with_name)
- if with_name == nil then return end
- local schedul_hander = self.schedulers[with_name]
- if schedul_hander ~= nil then
- self.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)
- end
- self.schedulers = {}
- end
-
- function GlobalTimeTicket:hasTicket(with_name)
- local schedul_hander = self.schedulers[with_name]
- if schedul_hander ~= nil then
- return true
- end
- return false
- end
-
- function GlobalTimeTicket:getSchedulers()
- return self.schedulers
- end
-
- -- 停止定时器
- function GlobalTimeTicket:stop()
- self.is_stop = true
- self:removeAll()
- end
-
- ----------------------------------------------------------------------------------- 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 = 0
- if table then
- for _ in pairs(table) do
- len = len + 1
- end
- end
- return len
- end
-
- function 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 then
- scroll_view:setDirection(ccui.ScrollViewDir.horizontal)
- else
- scroll_view:setDirection(ccui.ScrollViewDir.vertical)
- end
-
- scroll_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) then
- parent_wnd:addChild(scroll_view)
- end
- return scroll_view
- end
-
- svUtil = class("svUtil", function()
- return ccui.Layout:create()
- end)
-
- function svUtil:ctor(parent, pos, dir, start_pos, size, setting, ap