• 注册
    • 总打赏排行
    • 今日收益排行
  • 小黄人
    小黄人
    如有问题,请发帖交流,私聊我可能看不到。
  • 小黄人
    小黄人
    如有问题,请发帖交流,私聊我可能看不到。
    • 今日签到
    • 累计签到

    暂没有数据

  • 小黄人
    小黄人
    如有问题,请发帖交流,私聊我可能看不到。
  • 冰龙
    冰龙
    冰龙
  • tianshi777
  • 隔壁老王
  • zxc583700871
  • mstscexe
  • 挽弦暮笙
  • kankan
  • 黑色星期天
  • beyourself
  • 查看作者
  • tab横向导航变纵向

    感谢群内大佬指点,自己找半天没找到关键代码。

    LUA_RET={
    {n="panel_bg",id=2,parent=1,w=931,h=599,res="img_rec_bg",type=1,tag=215,ss=true,sx=66,sy=66,sw=68,sh=68,v=true,},
    --{n="Image_1",id=3,parent=1,w=419,h=52,ax=0.5,ay=0.5,x=200,y=580,res="img_biaoti",type=1,tag=751,v=true,},
    {n="Image_1",id=3,parent=1,w=419,h=52,ax=0.5,ay=0.5,x=465,y=585,res="img_biaoti",type=1,tag=751,v=true,},
    {n="panel_title",id=4,parent=1,w=76,h=43,ax=0.5,ay=0.5,x=465,y=584,res="img_role_title",type=1,tag=217,v=true,},
    --{n="panel_title",id=4,parent=1,w=76,h=43,ax=0.5,ay=0.5,x=55,y=580,res="img_role_title",type=1,tag=217,v=true,},
    --左页签w=80,h=585--x=310,y=-10
    {n="box_tab",id=5,parent=1,w=80,h=585,ax=1.0,x=920,y=-16,type=0,tag=216,t=true,v=true,ud="tabv(角色,时装,内功,转生,宝石,元神,法宝)",},--,称号,新增
    {n="sanjiao_1",id=6,parent=1,w=41,h=40,ax=0.5,ay=0.5,x=310,y=-10,res="img_waikuangjiantou_you",type=1,tag=250,v=true,},
    {n="sanjiao_2",id=7,parent=1,w=41,h=40,ax=0.5,ay=0.5,x=905,y=20,res="img_waikuangjiantou_zuo",type=1,tag=251,v=true,},
    {n="PanelAvatar",id=1,type=0,parent=0,w=931,h=599,},
    }

    关键位置代码位于app/ui/uitab.lua下面

    --页签控件
    UITab = class("UITab",function()
    	return ccui.Widget:create()
    end)
    local defStyle = {
        horizon={
            normal = "tab_dz",
            select = "tab_dz_sel",
            ncolor = game.getColor(0xf1e8d0),
            scolor = game.getColor(0xf1e8d0),
            startX = 4,
            itemsMargin = 10,
            fontSize = 20,
            scale = false,
            action = false,
            labelStyle = "horizon", --竖向文字,横向文字
        },
        vertical={
            normal = "tab_v1",
            select = "tab_v2",
            ncolor = game.getColor(0xc3ad88),
            scolor = game.getColor(0xfddfae),
            startY = -4,
            itemsMargin = 10,
            fontSize = 20,
            scale = true,
            offx=2,
            offy=0,
            labelStyle = "vertical", --竖向文字,横向文字
        }
    }
    local defStyle2 = {
        horizon={
            normal = "tab_dz",
            select = "tab_dz_sel",
            ncolor = game.getColor(0xf1e8d0),
            scolor = game.getColor(0xf1e8d0),
            startX = 4,
            itemsMargin = 5,--间隔
            fontSize = 20,
            scale = false,
            action = false,
            labelStyle = "horizon", --横向文字
        },
        vertical={
            -- normal = "tab_v5",
            -- select = "tab_v6",
            -- ncolor = game.getColor(0xc3ad88),
            -- scolor = game.getColor(0xfddfae),
            -- startY = -4,
            -- itemsMargin = 5,
            -- fontSize = 20,
            -- scale = true,
            --offx=2,
            --offy=0,
            --labelStyle = "vertical", --竖向文字
    		normal = "tab_dz2",
            select = "tab_dz2_sel",
            ncolor = game.getColor(0xc3ad88),
            scolor = game.getColor(0xfddfae),
            startX = 4,
            itemsMargin = 1,
            fontSize = 20,
            scale = false,
            action = false,
            labelStyle = "vertical", --横向文字
        }
    }
    
    function UITab:ctor(params)
    	self._items = {};
    	self._tabEventListener = nil;
    	self._listTab = nil;
        self._curIndex = params.defaultTab or 1;
    
    	self._ntcolor = nil;
    	self._stcolor = nil;
    
    	self.style = params.style or "horizon"
    	self.titles = params.titles or {}
        local con = defStyle[params.style] or defStyle["horizon"]
    	self._conf = clone(con);
        if params.normal then
            self._conf.normal = params.normal
        end
        if params.select then
            self._conf.select = params.select
        end
        if params.itemsMargin then
            self._conf.itemsMargin = params.itemsMargin
        end
        self:setAnchorPoint(0, 0)
        self:setContentSize(params.size)
    	self:initTab(self._conf)
    end
    
    function UITab:initTab( conf )
    	
        local item, lblTitle, normalRes = conf.normal
        local selectedRes = conf.select;
        self._nres = conf.normal;
        self._sres = conf.select;
        self._ntcolor = conf.ncolor;
        self._stcolor = conf.scolor;
        self._fontSize = conf.fontSize;
        self._labelStyle = conf.labelStyle
        for i,v in ipairs(self.titles) do
            item = ccui.Button:create();
    		item:loadTextureNormal(self._nres,ccui.TextureResType.plistType)
    		item:loadTexturePressed(self._sres,ccui.TextureResType.plistType)
            item:setTouchEnabled(true);
            item:setTag(i);
            item:setName("tab"..i);
            -- if math.ceil(#v/3)>=4 then
            --     self._fontSize = 16
            -- -- end
    		
            local itemsize = item:getContentSize()
            local params = {text= v, fontSize= self._fontSize,anchor = cc.p(0.5,0.5),outlineColor = cc.c3b(0,0,0),outlineStrength = 1}
            if self.style == "horizon" then
            	item:setAnchorPoint(0, 0);
            	item:setPosition(conf.startX + (item:getContentSize().width + conf.itemsMargin) * (i-1),0);
            else
                if itemsize.width<itemsize.height then
                    params.contentSize = cc.size(self._fontSize,math.ceil(#v/3)*(self._fontSize+4))
                end
            	item:setAnchorPoint(1, 0.5);
            	item:setPosition(self:getContentSize().width, conf.startY - 0.5 * item:getContentSize().height - (item:getContentSize().height + conf.itemsMargin) * (i-1));
            end
    		
    		
            -- local itemsize = item:getContentSize()
            -- local params = {text= v, fontSize= self._fontSize,anchor = cc.p(0.5,0.5),outlineColor = cc.c3b(0,0,0),outlineStrength = 1}
            -- if self.style == "horizon" then
            	-- item:setAnchorPoint(0, 0);
            	-- item:setPosition(conf.startX + (item:getContentSize().width + conf.itemsMargin) * (i-1),0);
            -- else
                -- if itemsize.width<itemsize.height then
                    -- params.contentSize = cc.size(self._fontSize,math.ceil(#v/3)*(self._fontSize+4))
                -- end
            	-- --item:setAnchorPoint(1, 0.5);
            	-- --item:setPosition(self:getContentSize().width, conf.startY - 0.5 * item:getContentSize().height - (item:getContentSize().height + conf.itemsMargin) * (i-1));
            	-- item:setAnchorPoint(0, 0);
            	-- item:setPosition(conf.startX + (item:getContentSize().width + conf.itemsMargin) * (i-1),0);
    		-- end
            self:addChild(item);
            lblTitle = util.newUILabel(params);
            lblTitle:setPosition(itemsize.width/2+(conf.offx or 0), itemsize.height/2+(conf.offy or 0));
            lblTitle:setName("lblTitle");
            lblTitle:setColor(self._ntcolor);
            item:addChild(lblTitle);
    
            table.insert(self._items,item);
            item:addClickEventListener(handler(self,self.onTabButtonClicked))
            if self._conf.action then
                item:setPressedActionEnabled(true)
                item:setZoomScale(-0.12)
            end
        end
    end
    
    function UITab:onTabButtonClicked(sender, noCb)
        local lblTitle;
        for i,v in ipairs(self._items) do
            lblTitle = v:getChildByName("lblTitle")
            if i ~= sender:getTag() then
                v:setBrightStyle(ccui.BrightStyle.normal);
                v:setTouchEnabled(true);
                lblTitle:setColor(self._ntcolor);
                v:setScale(1.0)
            else
                self._curIndex = i
                v:setBrightStyle(ccui.BrightStyle.highlight);
                v:setTouchEnabled(false)
                lblTitle:setColor(self._stcolor);
                if self._conf.scale then
                    v:setScale(1.0)
                end
            end
        end
        if not noCb then
            if self._tabEventListener then
                self._tabEventListener(sender);
            end
        end
    end
    
    function UITab:addTabEventListener( callBack )
        self._tabEventListener = callBack;
        return self
    end
    
    function UITab:setSelectedTab( index )
    	if self._items[index] then
    		self:onTabButtonClicked(self._items[index]);
    	end
        return self
    end
    --仅仅设置状态
    function UITab:setTabSelected(index)
        if self._items[index] then
            self:onTabButtonClicked(self._items[index], true)
        end
        return self
    end
    
    function UITab:getCurIndex()
        return self._curIndex
    end
    
    function UITab:hideTab( indexList )
        if util.isNumber(indexList) then
            indexList = {indexList}
        end
        local conf = self._conf
        local showi = 0
        for i,v in ipairs(self._items) do
            if table.indexof(indexList, i) then
                v:hide()
            else
                v:show()
                showi = showi + 1
            end
            if self.style == "horizon" then
                v:setAnchorPoint(0, 0);
                v:setPosition(conf.startX + (v:getContentSize().width + conf.itemsMargin) * (showi-1),0);
            else
                --v:setAnchorPoint(1, 0.5);
                --v:setPosition(self:getContentSize().width, conf.startY - 0.5 * v:getContentSize().height - (v:getContentSize().height + conf.itemsMargin) * (showi-1));
                v:setAnchorPoint(0, 0);
                v:setPosition(conf.startX + (v:getContentSize().width + conf.itemsMargin) * (showi-1),0);
    		end
        end
        return self
    end
    
    function UITab:setTabVisible(index,visible)
        for i,v in ipairs(self._items) do
            if i==index then
                v:setVisible(visible);
            end
        end
        return self
    end
    
    function UITab:getDescription()
        return self.__cname;
    end
    
    function UITab:setTabRes(normal,selected,textureType)
        self._nres = normal;
        self._sres = selected;
        textureType = textureType or ccui.TextureResType.plistType
        local conf = self._conf
        for i,v in ipairs(self._items) do
            v:loadTextureNormal(self._nres,textureType)
            v:loadTexturePressed(self._sres,textureType)
            if self.style == "horizon" then
                v:setAnchorPoint(0, 0);
                v:setPosition(conf.startX + (v:getContentSize().width + conf.itemsMargin) * (i-1),0);
            else
                --v:setAnchorPoint(1, 0.5);
                --v:setPosition(self:getContentSize().width, conf.startY - 0.5 * v:getContentSize().height - (v:getContentSize().height + conf.itemsMargin) * (i-1));
                v:setAnchorPoint(0, 0);
                v:setPosition(conf.startX + (v:getContentSize().width + conf.itemsMargin) * (i-1),0);
            end
            -- v:getWidgetByName("lblTitle"):setPosition(v:getContentSize().width/2,v:getContentSize().height/2)
        end
        self:setFontSize(self._fontSize)
        return self
    end
    
    function UITab:setTabColor(ncolor,scolor)
        self._ntcolor = ncolor
        self._stcolor = scolor
        for i,v in ipairs(self._items) do
            lblTitle = v:getChildByName("lblTitle")
            if i ~= self._curIndex then
                lblTitle:setColor(self._ntcolor);
            else
                lblTitle:setColor(self._stcolor);
            end
        end
        return self
    end
    
    function UITab:setFontSize(size)
        -- if self._fontSize ~= size then
            self._fontSize = size
            local lblTitle,string,itemsize,contentSize
            for i,v in ipairs(self._items) do
                lblTitle = v:getWidgetByName("lblTitle")
                if self.style == "vertical" then
                    string = lblTitle:getString()
                    itemsize = v:getContentSize()
                    if itemsize.width<itemsize.height then
                        local contentSize = cc.size(self._fontSize,math.ceil(#string/3)*(self._fontSize+4))
                        lblTitle:setContentSize(contentSize):setTextAreaSize(contentSize)
                    else
                        local contentSize = cc.size(math.ceil(#string/3)*(self._fontSize+2),self._fontSize)
                        lblTitle:setContentSize(contentSize):setTextAreaSize(contentSize)
                    end
                end
                lblTitle:setFontSize(size):setPosition(v:getContentSize().width/2,v:getContentSize().height/2)
            end
        -- end
        return self
    end
    function UITab:setOffsetPosition(pos)
        if pos then
             for i,v in ipairs(self._items) do
                lblTitle = v:getWidgetByName("lblTitle")
                if lblTitle then
                    lblTitle:setFontSize(size):setPosition(v:getContentSize().width/2 + pos.x,v:getContentSize().height/2 + pos.y)
                end
            end
        end
    end
    function UITab:setScaleEnabled(value)
        if util.isBool(value) then
            self._conf.scale = value
            for i,v in ipairs(self._items) do
                if self._curIndex == i and value then
                    v:setScale(1.0)
                else
                    v:setScale(1)
                end
            end
        end
        return self
    end
    
    function UITab:setItemMargin(value)
        if self._conf.itemsMargin ~= value then
            self._conf.itemsMargin = value
            for i,v in ipairs(self._items) do
                if self.style == "horizon" then
                    v:setAnchorPoint(0, 0);
                    v:setPosition(self._conf.startX + (v:getContentSize().width + self._conf.itemsMargin) * (i-1),0);
                else
                    --v:setAnchorPoint(1, 0.5);
                    --v:setPosition(self:getContentSize().width, self._conf.startY - 0.5 * v:getContentSize().height - (v:getContentSize().height + self._conf.itemsMargin) * (i-1));
    				v:setAnchorPoint(0, 0);
    				v:setPosition(self._conf.startX + (v:getContentSize().width + self._conf.itemsMargin) * (i-1),0);
    			end
            end
        end
        return self
    end
    
    function UITab:getItemByIndex(index)
        return self._items[index]
    end
    function UITab:setTouchEnabled(enable)
        for k,v in pairs(self._items) do
            v:setTouchEnabled(enable)
        end
        getmetatable(self).setTouchEnabled(self,enable)
        return self
    end
    return UITab;


    76.47% (26) 次Baidu访问
    17.65% (6) 次Google访问
    5.88% (2) 次sm访问
    • 蜘蛛:Baidu,26次,更新时间:2020-10-20 09:14:35
    • 蜘蛛:Google,6次,更新时间:2020-09-13 19:58:45
    • 蜘蛛:sm,2次,更新时间:2020-07-04 19:41:37
  • 0
  • 0
  • 0
  • 18
  • 请登录之后再进行评论

    登录
  • 做任务
  • 实时动态
  • 偏好设置
  • 返回顶部
  • 单栏布局 侧栏位置: