|
如果需要缓动效果可以使用该函数
---缓动
---@param easeType string 缓动方式
---@param currentTime number 缓动当前进度
---@param startValue number 初始值
---@param changeValue number 目标值
---@param duration number 缓动总进度
---@param power number 指数
function Util.ease(_type,currentTime, startValue, changeValue, duration, power)
currentTime = currentTime:float()
startValue = startValue:float()
changeValue = changeValue:float()
duration = duration:float()
changeValue = changeValue - startValue
--print(_type,currentTime, startValue, changeValue, duration)
local ease = {
['线性'] = function(currentTime, startValue, changeValue, duration)
return currentTime * changeValue / duration + startValue
end,
['缓入'] = function(currentTime, startValue, changeValue, duration, power)
if not power then power = 2 end
currentTime = currentTime / duration
return changeValue * (currentTime ^ power) + startValue
end,
['缓出'] = function(currentTime, startValue, changeValue, duration, power)
if not power then power = 2 end
if power > 2 then
currentTime = currentTime / duration - 1
if Util.isEvenNum(power) then
return changeValue * (currentTime ^ power - 1) + startValue
else
return changeValue * (currentTime ^ power + 1) + startValue
end
else
currentTime = currentTime / duration
return -changeValue * currentTime * (currentTime - 2) + startValue
end
end,
}
return ease[_type](currentTime, startValue, changeValue, duration, power)
end
搭配计时器使用
---缓动计时器
---@param easeType string 缓动方式
---@param duration float 总时间
---@param startValue float 起始位置
---@param changeValue float 变化量
---@param action function 执行函数
function ac.ease_timer(easeType,startValue,changeValue,duration,action)
---当前时间
local currentTime = 0
ac.loop(0.03,function(self)
currentTime = currentTime + 0.03
action(ac.ease(easeType,currentTime,startValue,changeValue,duration))
if currentTime >= duration then
self:remove()
end
end)
end
|
|