UITabBarView = UITabBarView or BaseClass(BaseComponent) local UITabBarView = UITabBarView local math_abs = math.abs function UITabBarView:__init(parent_wnd,prefab_asset,layer_name,view_style,is_scroll) self.view_style = view_style self.is_scroll = is_scroll end --[[@ 功能: 参数: tabbar_list 标签名字数组 ({name, ...}) switch_callback 切换回调 tabbar_style 标签样式,默认为1 extra_arg_table 额外参数列表 返回值: ]] function UITabBarView:SetData(tabbar_list, switch_callback, tabbar_style, linebg_size_callback,extra_arg_table) self.curr_tabBtn_index = 0 self.tabbar_item_list = {} self.linebg_size_callback = linebg_size_callback self.tabbar_list = tabbar_list self.switch_callback = switch_callback self.tabbar_style = tabbar_style self.extra_arg_table = extra_arg_table self.parms = UITabButton.Style[self.tabbar_style or 1] or UITabButton.Style[1] self:CreateGameObject(UIType.Image3, "tab_line_bg") end function UITabBarView:LoadSuccess() self.line_bg = self.gameObject:GetComponent("Image") self:LoadTabBarPrefab() self:UpdateLineHeight() end function UITabBarView:UpdateLineHeight( ) self.gameObject.transform.anchorMin = Vector2(0.5, 1) self.gameObject.transform.anchorMax = Vector2(0.5, 1) self.gameObject.transform.pivot = Vector2(0.5, 1) local len = #self.tabbar_list local height = math_abs(self.parms.offsetY) + len * self.parms.spacing + 30 -- SetAnchoredPosition(self.gameObject.transform, 0,0)--0.5,1的轴点的local不准确 还是用anchored if self.linebg_size_callback then new_height = height > 580 and 580 or height self.linebg_size_callback(self.parms.line_width,new_height,height) else SetLocalPositionX(self.gameObject.transform, self.parms.line_offsetX) self.gameObject.transform.sizeDelta = Vector2(self.parms.line_width, height) end end function UITabBarView:LoadTabBarPrefab() local vo = nil local item = nil local click_callback = function(index) self:SetTabBarIndex(index) end local item_len = #self.tabbar_list for i = 1,#self.tabbar_list do vo = self.tabbar_list[i] item = self.tabbar_item_list[i] if item == nil then item = UITabButton.New(self.parent_transform, nil, self.layer_name , self.view_style, self.tabbar_style, self.is_scroll) self.tabbar_item_list[i] = item end item:SetData(i, vo, click_callback, item_len, self.extra_arg_table) end for i=#self.tabbar_list + 1,#self.tabbar_item_list do self.tabbar_item_list[i]:SetVisible(false) end --延迟等所有的页签设置完再合批 -- local function on_delay( ) -- self:DoBatch() -- end -- setTimeout(on_delay, 0.5) end function UITabBarView:DoBatch() -- if self.tabbar_style == 2 then -- do return end -- end if IsNull(self.parent_transform) then return end if not self.batch_cont then self.batch_cont = UiFactory.createChild(self.parent_transform, UIType.EmptyObject, "batch_cont") end if not self.batch_cont then return end for k,v in pairs(self.tabbar_item_list) do v:DoBatch(self.batch_cont.transform) end for k,v in pairs(self.tabbar_item_list) do if v.text_transform then v.text_transform:SetAsLastSibling() end end self.batch_cont.transform:SetAsLastSibling() end function UITabBarView:SetTabBarIndex(index,is_force,ignore_call_back) if self.curr_tabBtn_index == index and not is_force then return end local tab_btn for k,v in pairs(self.tabbar_item_list) do if v.tab_id == self.curr_tabBtn_index then tab_btn = v end end if tab_btn then tab_btn:SetSelect(false) end self.curr_tabBtn_index = index local tab_btn local pos_count = 0 for k,v in pairsByKeys(self.tabbar_item_list) do pos_count = pos_count + 1 if v.tab_id == self.curr_tabBtn_index then tab_btn = v break end end if tab_btn then tab_btn:SetSelect(true) end if self.gameObject then self.gameObject.transform:SetAsFirstSibling() end if not ignore_call_back and self.switch_callback then if not self.first_open_loaded and self.view_style == UITabWindow.SizeSmall then--防止页签过多 一开始看不到页签 self.first_open_loaded = true local max_num = #self.tabbar_item_list if pos_count <= 6 then --前六个 SetAnchoredPositionY(self.parent_transform, -48) else local show_left_pos = (pos_count-6) if pos_count == max_num then --最后一个 show_left_pos = (pos_count-1) end show_left_pos = (show_left_pos+6) > max_num and (max_num-6) or show_left_pos SetAnchoredPositionY(self.parent_transform,show_left_pos * (self.parms.spacing+30)) end end self.switch_callback(index) end end function UITabBarView:ClickTabItemHandler( id ) if self.curr_tabBtn_index == id then return end local tab_btn for k,v in pairs(self.tabbar_item_list) do if v.tab_id == self.curr_tabBtn_index then tab_btn = v end end if tab_btn and tab_btn.transform then tab_btn:SetSelect(false) end self.curr_tabBtn_index = id item = self:GetTabItem(id) if item ~= nil then item:SetSelected(true) end local tab_btn for k,v in pairs(self.tabbar_item_list) do if v.tab_id == self.curr_tabBtn_index then tab_btn = v end end if tab_btn and tab_btn.transform then -- print("HWR:UITabbarView [97]self.curr_tabBtn_index:,index ",self.curr_tabBtn_index,index) tab_btn:SetSelect(true) end if self.gameObject then self.gameObject.transform:SetAsFirstSibling() end end function UITabBarView:GetCurrentSelectIndex() return self.curr_tabBtn_index end function UITabBarView:ShowRedPoint(index,bool) for k,v in pairs(self.tabbar_item_list) do if index == v.tab_id then v:ShowRedPoint(bool) break end end -- local tab_btn = self.tabbar_item_list[index] -- if tab_btn then -- tab_btn:ShowRedPoint(bool) -- end end function UITabBarView:ShowRedPointWithNum(index, num, force_show) for k,v in pairs(self.tabbar_item_list) do if index == v.tab_id then v:ShowRedPointWithNum(num, force_show) break end end end function UITabBarView:SetSpeacialImg(index, state) for k,v in pairs(self.tabbar_item_list) do if index == v.tab_id then v:SetSpeacialImg(state) break end end end function UITabBarView:SetTextSize(font_size) for k,v in pairs(self.tabbar_item_list) do v:SetTextSize(font_size) end end function UITabBarView:GetTabbarBtn( index ) local tab_btn = self.tabbar_item_list[index] return tab_btn end function UITabBarView:__delete() self.curr_tabBtn_index = 0 for i,item in pairs(self.tabbar_item_list) do item:DeleteMe() end self.tabbar_item_list = {} end function UITabBarView:RefeshTabBar( ) self:LoadTabBarPrefab() end function UITabBarView:RefreshTabData( tabbar_list, switch_callback, tabbar_style ) self.tabbar_list = tabbar_list or self.tabbar_list self.switch_callback = switch_callback or self.switch_callback self.tabbar_style = tabbar_style self.parms = UITabButton.Style[self.tabbar_style or 1] or UITabButton.Style[1] local vo = nil local item = nil local click_callback = function(index) self:SetTabBarIndex(index) end for k,v in pairs(self.tabbar_item_list) do v:SetVisible(false) -- 重置tab_id,避免隐藏掉的页签按钮跟某个存在的页签按钮id相同导致不能正常点亮 v.tab_id = -1 end local item_len = #self.tabbar_list for i = 1,#self.tabbar_list do vo = self.tabbar_list[i] item = self.tabbar_item_list[i] if item == nil then item = UITabButton.New(self.parent_transform, nil, self.layer_name , self.view_style, self.tabbar_style, self.is_scroll) self.tabbar_item_list[i] = item end item:SetVisible(true) item:SetData(i, vo, click_callback, item_len) end self:UpdateLineHeight() self:DoBatch() end