--[[ @description:自定义多级选项卡 @example: local data = {["_1"] = {name = "一级选项卡1", ...}, ["_1_1"] = {name = "二级选项卡1", ...}} or local data = {["_1"] = "一级选项卡1", ["_1_1"] = "二级选项卡1"} self.tabs = CustomVerTabs.New(self.tab_parent_tra, co.TableXY(265, 599), CusVerMainTabsTemplate, co.TableXY(256, 70), co.TableXY(0, 6), co.TableXY(0, 3), co.TableXY(0, 3), UIVerTabSubBtn, co.TableXY(0, 3)) local function call_back(main_index, sub_index) end self.tabs:SetTabs(data, call_back, true, true) --]] CustomVerTabs = CustomVerTabs or BaseClass(BaseComponent) local CustomVerTabs = CustomVerTabs local SetSizeDelta = SetSizeDelta local SetAnchoredPosition = SetAnchoredPosition --[[ @scroll_rect: 选项卡区域 @main_tabs_class: 自定义一级选项卡 @main_tabs_btnSize:一级选项卡按钮大小 @main_tabs_padding:一级选项卡顶部,左侧内边距 @main_tabs_spacing: 一级选项卡间距 @subCon_offset:一级选项卡子内容偏移 @sub_tabs_class: 自定义的二级选项卡 @sub_tabs_spacing: 二级选项卡间距 --]] function CustomVerTabs:__init(parent_transform, scroll_rect, main_tabs_class, main_tabs_btnSize, main_tabs_padding, main_tabs_spacing, subCon_offset, sub_tabs_class, sub_tabs_spacing, arrow_pos) self.scroll_rect = scroll_rect or co.TableXY(202, 561) self.main_tabs_class = main_tabs_class self.main_tabs_btnSize = main_tabs_btnSize or co.TableXY(189, 66) self.main_tabs_padding = main_tabs_padding or co.TableXY(0, 0) self.main_tabs_spacing = main_tabs_spacing or co.TableXY(0, 0) self.subCon_offset = subCon_offset self.arrow_pos = arrow_pos self.sub_tabs_class = sub_tabs_class self.sub_tabs_spacing = sub_tabs_spacing self.quick_select = true self.select_main_index = nil self.btn_list = {} self.btn_load_complete = false self:CreateGameObject(UIType.ScrollView, "ScrollView") end function CustomVerTabs:__delete() for i,v in ipairs(self.btn_list) do v:DeleteMe() v = nil end self.btn_list = {} self:CancelTimer() end function CustomVerTabs:LoadSuccess() local vec2 = Vector2(0, 1) self.transform.anchorMax = vec2 self.transform.anchorMin = vec2 self.transform.pivot = vec2 SetAnchoredPosition(self.transform, 0, 0) SetSizeDelta(self.transform, self.scroll_rect.x, self.scroll_rect.y) self.scroll_view_content = self:GetChild("Viewport/Content") self.viewport_mask = self:GetChild("Viewport"):GetComponent("Mask") self.viewport_image = self:GetChild("Viewport"):GetComponent("Image") end function CustomVerTabs:SetTabs(bar_list, call_back, select_anything, force_size, quick_select) if bar_list == nil or TableSize(bar_list) == 0 then return end self.quick_select = quick_select == nil and true or quick_select self:ResetTabsIndex() self:RefreshTabsList(bar_list, call_back, select_anything, force_size) --self:RefreshTabsPos() self:ResetTabsConPos() end function CustomVerTabs:ResetTabsIndex() if self.select_main_index then self:CancelSelectTabs(self.select_main_index) self:RefreshTabsPos() end self.select_main_index = nil end function CustomVerTabs:RefreshTabsList(bar_list, call_back, select_anything, force_size) self:CancelTimer() local count = 0 local gap_y = self.main_tabs_padding.y local gap_x = self.main_tabs_padding.x + self.main_tabs_spacing.x local function refresh_view_func() if self._use_delete_method then return end count = count + 1 local index = count local tabs_index = "_" .. index if bar_list[tabs_index] then local tabs_btn = self.btn_list[index] if tabs_btn == nil then tabs_btn = self.main_tabs_class.New(self.scroll_view_content, self.main_tabs_btnSize, self.subCon_offset, self.sub_tabs_class, self.sub_tabs_spacing, self.arrow_pos) self.btn_list[index] = tabs_btn end tabs_btn:SetTabsDetail(bar_list, index, call_back, select_anything, force_size) tabs_btn:SetVisible(true) local function onBtnClickHandler(target) self:SetSelectTabs(index) end if tabs_btn.button then AddClickEvent(tabs_btn.button, onBtnClickHandler, 2) end tabs_btn:SetPosition(gap_x, -gap_y) gap_y = gap_y + tabs_btn.btn_height + self.main_tabs_spacing.y else for i = count, #self.btn_list do self.btn_list[i]:SetVisible(false) end self:RefreshTabsPos() self:CancelTimer() self.btn_load_complete = true if self.redDot_list then for k, v in pairs(self.redDot_list) do self:ShowRedDot(k, v) end self.redDot_list = nil end if self.cache_select then self.cache_select = false self:SetSelectTabs(self.cache_main_index,self.cache_sub_index,self.cache_force_show) end end end self.view_timer_id = GlobalTimerQuest:AddPeriodQuest(refresh_view_func,0.04) refresh_view_func() end function CustomVerTabs:CancelTimer() if self.view_timer_id then GlobalTimerQuest:CancelQuest(self.view_timer_id) self.view_timer_id = nil end end function CustomVerTabs:SetSelectTabs(main_index, sub_index, force_show) if not self.btn_load_complete then self.cache_select = true self.cache_main_index = main_index self.cache_sub_index = sub_index self.cache_force_show = force_show return end --print("CustomVerTabs:SetSelectTabs() main = "..tostring(main_index)..", sub = "..tostring(sub_index)) for i,v in ipairs(self.btn_list) do if v.index and v.index == main_index then if self.select_main_index ~= main_index and self.select_main_index ~= nil then self:CancelSelectTabs(self.select_main_index) end self.select_main_index = main_index v:SetSelectMode(main_index, sub_index, force_show, self.quick_select) sub_index = v.select_sub_index break end end self:RefreshTabsPos(main_index, sub_index) end function CustomVerTabs:CancelSelectTabs(index) if self.btn_list[index] then self.btn_list[index]:RefreshTabsSelectState(false) self.btn_list[index]:SetSelectSubIndex(0, 0) end --[[if index and index * self.main_tabs_btnSize.y > self.transform.sizeDelta.y / 2 then SetAnchoredPosition(self.scroll_view_content, 0, (index - 1) * self.main_tabs_btnSize.y) else SetAnchoredPosition(self.scroll_view_content, 0, 0) end--]] end --更新tabBar的位置 function CustomVerTabs:RefreshTabsPos(main_index, sub_index) local gap_y = self.main_tabs_padding.y local gap_x = self.main_tabs_padding.x + self.main_tabs_spacing.x local sub_tabs_height = self.sub_tabs_class and self.sub_tabs_class.real_height or 0 local subCon_offset_y = self.subCon_offset and self.subCon_offset.y or 0 local btn_height, off, h, sub_tabs_count for i,v in ipairs(self.btn_list) do if v:GetVisible() then btn_height = v.btn_height if v.show == true then sub_tabs_count = v:GetVisibleCount(true) off = self.sub_tabs_spacing and (sub_tabs_count - 1) * self.sub_tabs_spacing.y or 0 off = off < 0 and 0 or off + subCon_offset_y h = sub_tabs_count * sub_tabs_height + off v:SetPosition(gap_x, -gap_y) gap_y = gap_y + btn_height + self.main_tabs_spacing.y + h else v:SetPosition(gap_x, -gap_y) gap_y = gap_y + btn_height + self.main_tabs_spacing.y end end end SetSizeDelta(self.scroll_view_content, 0, gap_y + 40) if main_index and sub_index then sub_index = sub_index > 0 and sub_index - 1 or 0 --print("CustomVerTabs:RefreshTabsPos()",main_index,sub_index) local y_1 = (main_index - 1) * self.main_tabs_btnSize.y + sub_index * self.sub_tabs_class.Height local y_2 = self.scroll_view_content.sizeDelta.y - self.transform.sizeDelta.y if y_2 < self.main_tabs_btnSize.y then y_2 = 0 end SetAnchoredPosition(self.scroll_view_content, 0, y_1 > y_2 and y_2 or y_1) end end function CustomVerTabs:ResetTabsConPos() SetAnchoredPosition(self.scroll_view_content, 0, 0) end function CustomVerTabs:CancelAllRedDot() for i, v in pairs(self.btn_list) do v:ShowRedDot() end end function CustomVerTabs:ShowRedDot(index, index_list) if not self.btn_load_complete then self.redDot_list = self.redDot_list or {} self.redDot_list[index] = index_list return end if self.btn_list[index] and self.btn_list[index].ShowRedDot then self.btn_list[index]:ShowRedDot(index_list) end end -- 设置不规则遮罩显示 function CustomVerTabs:SetMaskImage(res) lua_resM:setOutsideImageSprite(self,self.viewport_image,res,false,function () self.viewport_mask.showMaskGraphic = true end) end function CustomVerTabs:SetDefaultSelected(index) self:SetSelectTabs(index) end