--[[ @description:垂直TabBar @example: code: local test_tab_data = { --选项卡配置, main_tab 代表一级选项卡,sub_tab 代表二级选项卡 [1] = {main_tab = "主Tab1", sub_tab = {"子Tab1", "子Tab2", "子Tab3", "子Tab4"}}, [2] = {main_tab = "主Tab2", sub_tab = {"子Tab1", "子Tab2", "子Tab3", "子Tab4"}}, [3] = {main_tab = "主Tab3", sub_tab = {"子Tab1", "子Tab2", "子Tab3", "子Tab4"}}, } local call_back = function (index, sub_index) --回调函数 end self.verTabBar = UIVerTabBar.New(self.scroll_content) self.verTabBar:SetTabBars(test_tab_data, call_back) self.verTabBar:SetSelectTab(1, 2) ]] UIVerTabBar = UIVerTabBar or BaseClass() local UIVerTabBar = UIVerTabBar function UIVerTabBar:__init(parent_wnd, sizeDelta, quick_select,type_id) self.parent_wnd = parent_wnd self.btn_list = {} self.cur_sub_index = nil self.new_source = false self.type_mode = type_id or 1 if quick_select == nil then quick_select = true end self.quick_select = quick_select if not sizeDelta and parent_wnd then sizeDelta = {} sizeDelta.x, sizeDelta.y = GetSizeDeltaXY(parent_wnd) end self:Load(sizeDelta) end function UIVerTabBar:__delete() self:DestroyBtnList() end function UIVerTabBar:DestroyBtnList() for i,v in ipairs(self.btn_list) do v:UnBindEvent() v:DeleteMe() v = nil end self.btn_list = {} end function UIVerTabBar:Load(sizeDelta) self.scroll_view = UiFactory.createChild(self.parent_wnd, UIType.ScrollView, "bar_scroll") self.scroll_view.transform.sizeDelta = sizeDelta and sizeDelta or Vector2(202, 561) self.scroll_view_content = self.scroll_view.transform:Find("Viewport/Content") self.scroll_view_viewport = self.scroll_view.transform:Find("Viewport") self:InitEvent() end function UIVerTabBar:InitEvent() end --设置选项卡数据 is_btn_need_select_img:大类按钮是否需要设置选中的背景图 function UIVerTabBar:SetTabBars(bar_list, call_back, select_anything, is_btn_need_select_img) if bar_list == nil or call_back == nil then return end for i,v in ipairs(self.btn_list) do v:SetVisible(false) end -- print("Nice:UIVerTabBar [67] self.new_source: ",self.new_source) for i,v in ipairs(bar_list) do local tab_btn = self.btn_list[i] if tab_btn == nil then tab_btn = UIVerTabBtn.New(self.scroll_view_content, nil, nil, self,self.type_mode,is_btn_need_select_img) self.btn_list[i] = tab_btn end --print("Nice:UIVerTabBar [74] self.new_source: ",self.new_source) tab_btn:SetTabDetail(v, i, call_back, select_anything) tab_btn:SetVisible(true) local x = 0 local y = -(i - 1) * UIVerTabBtn.Parms[self.type_mode].BTN_HEIGHT -5 -- local y = -i * UIVerTabBtn.Parms[self.type_mode].BTN_HEIGHT -5 tab_btn:SetPosition(x, y) local function onBtnClickHandler(target) self:SetSelectTab(tab_btn.index,tab_btn.sub_index,self.force_show) end if tab_btn.tab then AddClickEvent(tab_btn.tab, onBtnClickHandler,2) end end end --设置选中的选项卡 function UIVerTabBar:SetSelectTab(index,sub_index,force_show) self.force_show = force_show for i,v in ipairs(self.btn_list) do -- if self.new_source then -- v:SetNewSource() -- end if v.index and v.index == index then if not v.show then self:CancelSetSelectTab(index) end v:SetSelectMode(index, sub_index, force_show, self.quick_select) end end self:RefreshTabPos() end --取消所有的选中项 function UIVerTabBar:CancelSetSelectTab(index) for i,v in ipairs(self.btn_list) do v:RefreshBtnState(false) v:SetSelectSubIndex(0, 0) end self:RefreshTabPos() if index and index * UIVerTabSubBtn.Height > self.scroll_view.transform.sizeDelta.y / 2 then self.scroll_view_content.localPosition = Vector2(self.scroll_view_content.localPosition.x, (index - 1) * UIVerTabSubBtn.Height) else self.scroll_view_content.localPosition = Vector2(self.scroll_view_content.localPosition.x, 0) end end --更新tabBar的位置 function UIVerTabBar:RefreshTabPos() local gap = 2 for i,v in ipairs(self.btn_list) do if v.show == true then local h = #v.sub_btn_list * (UIVerTabBtn.Parms[self.type_mode].SUB_CON_HEIGHT+(UIVerTabBtn.Parms[self.type_mode].SUB_BTN_Y_SPACE or 10)) v:SetPosition(0, -gap) v:SetSubConSize(h) if h <= 10 then h = UIVerTabBtn.Parms[self.type_mode].BTN_Y_SPACE end gap = gap + UIVerTabBtn.Parms[self.type_mode].BTN_HEIGHT + h else v:SetPosition(0, -gap) gap = gap + UIVerTabBtn.Parms[self.type_mode].BTN_HEIGHT + UIVerTabBtn.Parms[self.type_mode].BTN_Y_SPACE end end self.scroll_view_content.sizeDelta = Vector2(202, gap) -- print("UIVerTabBar:RefreshTabPos()",(gap + 40)) end function UIVerTabBar:SetScrollViewSize(width,heigh) self.scroll_view.transform.sizeDelta = Vector2(width,heigh) end function UIVerTabBar:RefreshTabBtnState(index) for i,v in ipairs(self.btn_list) do if v.index == index then v:RefreshBtnState(v.show) end end self:RefreshTabPos() end function UIVerTabBar:GetTabBtnShowState(index) for i,v in ipairs(self.btn_list) do if v.index == index then return v.show end end end function UIVerTabBar:SetNewSource( ) self.new_source = true UIVerTabBtn.BTN_HEIGHT = 50 end --在回调函数中调用该函数,刷新选中状态 function UIVerTabBar:UpdateSelectState(index, sub_index) for i,v in ipairs(self.btn_list) do v:SetSelectState(false) for key,value in ipairs(v.sub_btn_list) do value:SetSelectState(false) end end if sub_index == 0 then self.btn_list[index]:SetSelectState(true) else self.btn_list[index].sub_btn_list[sub_index]:SetSelectState(true) end end function UIVerTabBar:SetImageTexture(asset_path,res_name,index_list) for i,v in ipairs(self.btn_list) do v:SetImageTexture() for key,value in ipairs(v.sub_btn_list) do value:SetImageTexture() end end -- PrintTable(index_list) if asset_path and res_name then for k,v in pairs(index_list) do self.btn_list[v.main_index]:SetImageTexture(asset_path,res_name) if v.sub_index ~= 0 then self.btn_list[v.main_index].sub_btn_list[v.sub_index]:SetImageTexture(asset_path,res_name) end end end end --传入主索引和所有要显示的子索引列表,取消红点只传入主索引 function UIVerTabBar:ShowRedDot(index, index_list) if self.btn_list[index] then self.btn_list[index]:ShowRedDot(index_list) end end function UIVerTabBar:CancelAllRedDot() for i, v in pairs(self.btn_list) do v:ShowRedDot() end end