--动态数字
--@txt 文本节点
--@old 初始的数字
--@count 要增加的数字
--@callback 数字调完之后的回调函数
--@concat 单位
function UICommon.addNumEffect(txt,old,count,callback,concat)
if count == 0 then
txt:setString(old..(type(concat) == "string" and concat or ""))
return
end
local time = 50
local nowTime = 0
local targetNum = old + count
local perNum = nil
local mod = count % time
local isSub = count < 0
if isSub then
count = count + (time - (mod == 0 and time or mod))
perNum = math.min(-1, math.floor(count / time))
else
count = count - mod
perNum = math.max(1, math.floor(count / time))
end
local function func()
old = old + perNum
txt:setString(old..(type(concat) == "string" and concat or ""))
if targetNum == old then
txt:unscheduleUpdate()
if callback then callback() end
return
end
nowTime = nowTime + 1
if nowTime >= time then
if isSub then
old = old - (time - (mod == 0 and time or mod))
else
old = old + mod
end
txt:setString(old..(type(concat) == "string" and concat or ""))
txt:unscheduleUpdate()
if callback then callback() end
end
end
txt:scheduleUpdateWithPriorityLua(func, 0)
end
--二级窗口弹出和关闭放大缩小的特效
function UICommon.openDialogAction(item,_scale,_dur)
if item == nil then return end
local scale =_scale or 0.9
local dur =_dur or 0.2
item:setScale(scale)
local act1 = cc.EaseBackOut:create(cc.ScaleTo:create(dur, 1))
item:runAction(act1)
end
function UICommon.closeDialogAction(item,callback)
if item == nil then return end
local act1 = cc.EaseBackIn:create(cc.ScaleTo:create(0.2, 0.9))
local act2 = cc.CallFunc:create(callback)
local seq = cc.Sequence:create(act1, act2)
item:runAction(seq)
end
-- 进度条动画
function UICommon.loadingTo(item,per,speedScale,reverse,callback)
if item == nil then return end
local perOld = item:getPercent()
if reverse == nil then reverse = false end
local flag = (reverse and perOld <= per) or (reverse == false and perOld >= per) -- 是否要再加载一遍
local speed = (per==perOld and 100 or math.abs(per-perOld))/60 * (speedScale or 1)
local function add()
perOld = perOld + speed
if flag then
if perOld >= 100 then
perOld = -speed
flag = false
end
else
if perOld >= per or perOld >= 100 then
item:unscheduleUpdate()
if callback then callback() end
perOld = per
end
end
item:setPercent(perOld)
end
local function sub()
perOld = perOld - speed
if flag then
if perOld <= 0 then
perOld = 100 + speed
flag = false
end
else
if perOld <= per or perOld <= 0 then
item:unscheduleUpdate()
if callback then callback() end
perOld = per
end
end
item:setPercent(perOld)
end
if reverse then -- 反向
item:scheduleUpdateWithPriorityLua(sub, 1)
else
item:scheduleUpdateWithPriorityLua(add, 1)
end
end
--输入框
--输入框统一通用
function UICommon.createEditBox(pnl,txt,color,holder,fontSize,maxNum,typeset,isSysFont,placeholderColor,offsetY)
local inputSize = pnl:getContentSize()
local editMes = ccui.EditBox:create(inputSize,"assetsRes/res/beijing/beijing130.png")
local function getTextNum()
local strFmt = tools.filterSpecChars(editMes:getText())
return strFmt
end
local function editBoxTextEventHandle(strEventName,pSender)
if strEventName == "began" then
elseif strEventName == "ended" then
elseif strEventName == "return" then
editMes:setText(getTextNum())
elseif strEventName == "changed" then
getTextNum()
end
end
editMes:setText(txt)
editMes:registerScriptEditBoxHandler(editBoxTextEventHandle)
editMes:setPosition(cc.p(inputSize.width/2, inputSize.height/2 + (offsetY and offsetY or 0)))
if not isSysFont then
editMes:setFontName(UICommon.getTTFName())
end
editMes:setFontSize(fontSize)
editMes:setFontColor(color)
editMes:setName("editMes")
if placeholderColor then
editMes:setPlaceholderFontColor(placeholderColor)
end
editMes:setPlaceholderFontSize(fontSize)
editMes:setPlaceHolder(holder)
if maxNum then
editMes:setMaxLength(maxNum)
end
editMes:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE)
pnl:addChild(editMes)
end
-- 重复放大缩小
function UICommon.repeScale(item,args1,args2)
args1 = UICommon.checkValid(args1)
args2 = UICommon.checkValid(args2)
local act1 = cc.ScaleTo:create(args1.time, args1.x,args1.y)
local act2 = cc.ScaleTo:create(args2.time, args2.x,args2.y)
local seq = cc.Sequence:create(act1, act2)
local action = cc.RepeatForever:create(seq)
item:runAction(action)
end
-- 重复渐隐和移动
function UICommon.repeatFadeAndMove(ltag, rtag)
if not ltag or not rtag then return end
local act1 = cc.MoveBy:create(1, {x=30})
local act11 = act1:reverse()
local act2 = cc.FadeOut:create(1)
local act3 = cc.MoveBy:create(0, {x=-30})
local act31 = act3:reverse()
local act4 = cc.FadeIn:create(0.5)
local seq1=cc.RepeatForever:create(cc.Sequence:create(cc.Spawn:create(act1,act2),act3,act4))
local seq2=cc.RepeatForever:create(cc.Sequence:create(cc.Spawn:create(act11,act2),act31,act4))
ltag:runAction(seq2:clone())
rtag:runAction(seq1:clone())
end
-- 重复旋转
function UICommon.repeatRotateCircle(item)
if item == nil then return end
local repeatAction = cc.RepeatForever:create(cc.RotateBy:create(12, 360))
item:runAction(repeatAction)
end
-- 重复变灰
function UICommon.repeatTintToGray(item)
if item == nil then return end
local action1 = cc.TintBy:create(0.5, -70, -70, -70)
local action1Back = action1:reverse()
local seq = cc.Sequence:create( action1, action1Back)
local action = cc.RepeatForever:create(seq)
item:runAction(action)
end
-- 停止变灰并恢复
function UICommon.stopTintAction(item)
if item == nil then return end
transition.stopTarget(item)
item:runAction(cc.TintTo:create(0, 255, 255, 255))
end
-- 向下移动并慢慢显示
function UICommon.moveDownAndFadeIn(item,delay)
if item == nil then return end
item:setOpacity(0)
item:moveBy({time = 0.6, y = -50})
item:fadeIn({time = 0.6})
end
-- 重复来回透明度
function UICommon.repeatFade(item)
if item == nil then return end
item:setOpacity(127)
local action1 = cc.FadeTo:create(0.5,255)
local action1Back = cc.FadeTo:create(0.5,127)
local seq = cc.Sequence:create( action1, action1Back)
local action = cc.RepeatForever:create(seq)
item:runAction(action)
end
-- 停止来回透明度
function UICommon.stopRepeatFade(item)
if item == nil then return end
transition.stopTarget(item)
item:setOpacity(0)
end
-- 盖章效果
function UICommon.stampAction(item,cb,delayTime)
if item == nil then return end
local scaleNum = item:getScale()
item:setScale(3 * scaleNum)
item:setVisible(true)
item:scaleTo({time = 0.2, scale = scaleNum, onComplete = function() delayCall(function() king:command("game.all.sealsomething") end, 50) end})
local delay = cc.DelayTime:create(math.max(delayTime or 0.25))
local seal = cc.CallFunc:create(function()
--音效
if cb~=nil then
cb();
end
end)
local seq = cc.Sequence:create(delay, seal)
item:runAction(seq)
end
-- 向上移动并消失
function UICommon.moveUpAndFadeOut(item,delay)
if item == nil then return end
local act0 = cc.FadeIn:create(0)
local act1 = cc.MoveBy:create(0.8, {y = 150})
local act2 = cc.FadeOut:create(0.4)
local act3 = cc.MoveBy:create(0, {y = -150})
local seq = cc.Sequence:create(act0, act1, act2, act3)
local act = transition.create(seq, {delay=delay or 0})
item:runAction(act)
end
-- 渐隐消失
function UICommon.delayAndFadeOut(item,delay)
if item == nil then return end
local act0 = cc.FadeIn:create(0)
local act1 = cc.DelayTime:create(delay)
local act2 = cc.FadeOut:create(0.8)
local act3 = cc.CallFunc:create(function()
item:removeSelf()
end)
local seq = cc.Sequence:create(act0, act1, act2, act3)
item:runAction(seq)
end
-- 重复上下移动
function UICommon.repeatUpAndDown(item,spd,dis)
if item == nil then return end
local act1 = cc.MoveBy:create(spd or 1.2, {y = dis or 15})
local seq = cc.Sequence:create(act1, act1:reverse())
local action = cc.RepeatForever:create(seq)
item:runAction(action)
end