cocos2d-lua 简单实用方法

--检测非法字符

function util.checkInvalidChar(str,symbol)
    str=str or ""

    local tmp,tmp1,tmp2
    local i=1

    while i <= #str do
        tmp = string.byte(str,i)
        i = i + 1

        if tmp < 192 and not symbol then
            if (tmp >= 48 and tmp <= 57) or (tmp>=65 and tmp<=90) or (tmp>=97 and tmp<=122) then
            else               
                return true
            end

            --192及以上为双字节字符
            --224~239为三字节汉字
            elseif tmp >= 228 and tmp < 240 then
                --此处因为汉字一从228开始,前面是其他符号可抛弃
                tmp1=string.byte(str,i)
                i=i+1
                if tmp1>191 or tmp1<128 then
                    return true
                end
                tmp2=string.byte(str,i)
                i=i+1
                if tmp2>191 or tmp2<128 then
                    return true
            end
        --240及以上为四字节字符
        elseif tmp>=240 then
            return true
        end
    end
end

----------------------------------------scrollview--------------------------------------------------------

function UITool.ShowListEX(ScrollViewObj,Data,CallBack,DIR)
    --做个兼容处理--
    if type(Data)=="table" then
        DataCount=table.nums(Data)
        ScrollViewObj["Data"]=Data
    else
        DataCount=Data
        ScrollViewObj["Data"]={}
    end
    ScrollViewObj:setBounceEnabled(false)
    ScrollViewObj:setScrollBarEnabled(false)
    ScrollViewObj:registerScriptHandler(function(e)
        if e=="exit" then  
            ScrollViewObj["ItemMode"]:release()
            for _,V in pairs(ScrollViewObj["ShowItemObjArr"]) do
                V:release()
            end
        end
    end)

if not ScrollViewObj["ItemMode"] then
        ScrollViewObj["ItemMode"]=ScrollViewObj:getChildren()[1]
        ScrollViewObj["ItemMode"]:retain()
    end
    if DIR=="H" then
       return UITool.ShowListHorScroll(ScrollViewObj,DataCount,CallBack)
    end
    local SVHeight=ScrollViewObj:getContentSize().height
    local ItemSize=ScrollViewObj["ItemMode"]:getContentSize()
    local InnerHeight=math.max(SVHeight,ItemSize.height*DataCount)
    ScrollViewObj:setInnerContainerSize(cc.size(ItemSize.width,InnerHeight)) --改变会触发onEvent事件--
    ScrollViewObj["DataCount"]=DataCount
    ScrollViewObj:removeAllChildren()

local Y=ScrollViewObj:getInnerContainerPosition().y
    ScrollViewObj:setInnerContainerPosition(cc.p(0,math.min(0,Y)))

    local MaxItemCount=math.ceil(SVHeight/ItemSize.height)+1
    ScrollViewObj["ShowItemObjArr"]=ScrollViewObj["ShowItemObjArr"] or {}
    local ItemCount=table.nums(ScrollViewObj["ShowItemObjArr"]) --已有--
    for i=ItemCount+1,math.min(MaxItemCount,DataCount) do
        local ItemObj=ScrollViewObj["ItemMode"]:clone()
        ItemObj:setAnchorPoint(cc.p(0,0))
        ItemObj:retain()
        ItemObj:setContentSize(ItemSize)
        table.insert(ScrollViewObj["ShowItemObjArr"],ItemObj)
    end
    local PreIndex = 0

ScrollViewObj.onEventCallBack=function(e,ForceRefresh)
        local Y=ScrollViewObj:getInnerContainerPosition().y
        local ScrollSpace=InnerHeight+Y-SVHeight
        local CurIndex=math.ceil(ScrollSpace/ItemSize.height)
        CurIndex=math.max(1,CurIndex)
        if PreIndex~=CurIndex or ForceRefresh then
            PreIndex=CurIndex
            for i=1,table.nums(ScrollViewObj["ShowItemObjArr"]) do
                ScrollViewObj["ShowItemObjArr"][i]:setVisible(false)
                if CurIndex+i-1<=ScrollViewObj["DataCount"] then

ScrollViewObj["ShowItemObjArr"][i]:setVisible(true)
                    CallBack(CurIndex+i-1,ScrollViewObj["ShowItemObjArr"][i],ScrollViewObj["Data"][CurIndex+i-1])
                    ScrollViewObj["ShowItemObjArr"][i]:setPositionY(InnerHeight-ItemSize.height*(CurIndex+i-1))
                    if not ScrollViewObj["ShowItemObjArr"][i]:getParent() then
                        ScrollViewObj:addChild(ScrollViewObj["ShowItemObjArr"][i])
                    end
                end
            end
        end
    end
    ScrollViewObj:onEvent(ScrollViewObj.onEventCallBack)
    ScrollViewObj.onEventCallBack()
end

--------------------------------------------------------------------------------------------------------------------------------------------------------

function UITool.ShowListHorScroll(ScrollViewObj,DataCount,CallBack)
    local SVWidth=ScrollViewObj:getContentSize().width
    local ItemSize=ScrollViewObj["ItemMode"]:getContentSize()
    local InnerWidth=math.max(SVWidth,ItemSize.width*DataCount)
    if ScrollViewObj["DataCount"]~=DataCount then --先只考虑刷新前后数量不变的情况--
        ScrollViewObj:setInnerContainerSize(cc.size(InnerWidth,ItemSize.height)) --改变会触发onEvent事件--
    end
    ScrollViewObj["DataCount"]=DataCount
    ScrollViewObj:removeAllChildren()

    local X=ScrollViewObj:getInnerContainerPosition().x
    ScrollViewObj:setInnerContainerPosition(cc.p(math.min(0,X),0))

local MaxItemCount=math.ceil(SVWidth/ItemSize.width)+1
    ScrollViewObj["ShowItemObjArr"]=ScrollViewObj["ShowItemObjArr"] or {}
    local ItemCount=table.nums(ScrollViewObj["ShowItemObjArr"]) --已有--
    for i=ItemCount+1,math.min(MaxItemCount,DataCount) do
        local ItemObj=ScrollViewObj["ItemMode"]:clone()
        ItemObj:setAnchorPoint(cc.p(0,0))
        ItemObj:retain()
        ItemObj:setContentSize(ItemSize)
        table.insert(ScrollViewObj["ShowItemObjArr"],ItemObj)
    end
    local PreIndex = 0

ScrollViewObj.onEventCallBack=function(e,ForceRefresh)
        local X=ScrollViewObj:getInnerContainerPosition().x
        local ScrollSpace=-1*X
        local CurIndex=math.ceil(ScrollSpace/ItemSize.width)
        CurIndex=math.max(1,CurIndex)
        if PreIndex~=CurIndex or ForceRefresh then
            PreIndex=CurIndex
            for i=1,table.nums(ScrollViewObj["ShowItemObjArr"]) do
                ScrollViewObj["ShowItemObjArr"][i]:setVisible(false)
                if CurIndex+i-1<=ScrollViewObj["DataCount"] then
                    ScrollViewObj["ShowItemObjArr"][i]:setVisible(true)

CallBack(CurIndex+i-1,ScrollViewObj["ShowItemObjArr"][i],ScrollViewObj["Data"][CurIndex+i-1])
                    ScrollViewObj["ShowItemObjArr"][i]:setPositionX(ItemSize.width*(CurIndex+i-2))
                    if not ScrollViewObj["ShowItemObjArr"][i]:getParent() then
                        ScrollViewObj:addChild(ScrollViewObj["ShowItemObjArr"][i])
                    end
                end
            end
        end
    end
    ScrollViewObj:onEvent(ScrollViewObj.onEventCallBack)
    ScrollViewObj.onEventCallBack()
end

-------------------------------------------------------------------------------------------------------------------------

function UITool.InitScrollViewPageMode(ScrollViewObj,BtnNextPage,BtnPrePage,CallBack)
    ScrollViewObj:jumpToTop()
    ScrollViewObj:setTouchEnabled(false)
    BtnNextPage:setVisible(true)
    BtnPrePage:setVisible(false)
    local SVHeight=ScrollViewObj:getContentSize().height
    local InnerHeight=ScrollViewObj:getInnerContainerSize().height
    local SumPage=math.ceil(InnerHeight/SVHeight)
    ScrollViewObj.getCurPageNum=function()
        local Y=ScrollViewObj:getInnerContainerPosition().y
        local ScrollSpace=InnerHeight+Y-SVHeight
        return math.floor(ScrollSpace/SVHeight)+1 
    end

ScrollViewObj.onPage=function(n)
        local Y=ScrollViewObj:getInnerContainerPosition().y
        ScrollViewObj:setInnerContainerPosition(cc.p(0,Y+SVHeight*n))
    end
    function HandleBtnVisible()
        local CurPageNum=ScrollViewObj.getCurPageNum()
        BtnNextPage:setVisible(CurPageNum<SumPage)
        BtnPrePage:setVisible(CurPageNum>1)
        if CallBack then
            CallBack(CurPageNum,SumPage)
        end
    end

BtnNextPage:addClickEventListener(function()
        ScrollViewObj.onPage( 1)
        HandleBtnVisible()
    end)
    BtnPrePage:addClickEventListener(function()
        ScrollViewObj.onPage(-1)
        HandleBtnVisible()
    end)
    if CallBack then
        CallBack(1,SumPage)
    end
end

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值