Skip to content

TweenInfo

描述

补间动画配置数据类型,用于配置补间动画(UITween)的播放参数,包括动画持续时间、缓动风格、缓动方向、循环次数、延迟时间和是否反转等。TweenInfo 通过 TweenService:Create 方法传递给补间动画系统,控制属性插值动画的播放方式和效果

属性

floatTime
动画持续时间(以秒为单位),用于指定补间动画从起始值到目标值的播放时长。默认值为0.0
EasingStyleEasingStyle
缓动风格枚举,用于控制属性值变化的数学曲线,影响动画的节奏和视觉效果。默认值为Linear
EasingDirectionEasingDirection
缓动方向枚举,用于控制缓动曲线在动画中的应用方式,影响动画的加速和减速表现。默认值为In
floatDelayTime
延迟时间(以秒为单位),用于指定补间动画开始播放前的等待时间。默认值为0.0,表示不延迟
intRepeatCount
循环次数,用于指定补间动画重复播放的次数。当值小于0时,补间动画会无限循环播放;当值等于0时,动画只播放一次;当值大于0时,动画会重复播放指定的次数。默认值为0
boolReverses
是否反转,用于指定补间动画完成目标值后是否反向插值。当设置为true时,动画在完成一次播放后会反向播放(从目标值回到起始值),与RepeatCount配合使用可以实现往返动画效果。默认值为false

静态函数

TweenInfoNew ()
创建TweenInfo对象,使用所有默认参数值。返回的TweenInfo对象所有属性均为默认值:Time=0.0,EasingStyle=Linear,EasingDirection=In,DelayTime=0.0,RepeatCount=0,Reverses=false
TweenInfoNew (float time)
创建TweenInfo对象,指定动画持续时间。参数time为动画持续时间(秒),其他参数使用默认值
TweenInfoNew (float time, EasingStyle easingStyle, EasingDirection easingDirection, float delayTime, int repeatCount, bool reverses)
创建TweenInfo对象,指定所有参数。参数说明:time为动画持续时间(秒),easingStyle为缓动风格枚举(EasingStyle),easingDirection为缓动方向枚举(EasingDirection),delayTime为延迟时间(秒),repeatCount为循环次数(小于0表示无限循环),reverses为是否反转(true/false)

代码示例

lua
--获取TweenService服务
local TweenService = game:GetService("TweenService")

--创建UI根节点
local root = SandboxNode.new("UIRoot", game.WorkSpace)
--创建按钮
local button = SandboxNode.new("UIButton", root)
button.Size = Vector2.new(100, 50)
button.Position = Vector2.new(200, 100)

--创建默认的TweenInfo(所有参数为默认值)
local default = TweenInfo.New()

--创建只指定时间的TweenInfo(持续0.5秒)
local timeChanged = TweenInfo.New(0.5)

--创建指定缓动风格的TweenInfo(持续0.5秒,Back回弹风格)
local easingStyled = TweenInfo.New(0.5, Enum.EasingStyle.Back, 0, 0, 0, false)

--创建指定缓动方向的TweenInfo(持续0.5秒,Back风格,In方向)
local easingDirected = TweenInfo.New(0.5, Enum.EasingStyle.Back, Enum.EasingDirection.In, 0, 0, false)

--创建循环播放的TweenInfo(重复4次)
local repeated = TweenInfo.New(0.5, Enum.EasingStyle.Back, Enum.EasingDirection.In, 0, 4, false)

--创建反转的TweenInfo(完成目标后会反向插值)
local reverses = TweenInfo.New(0.5, Enum.EasingStyle.Back, Enum.EasingDirection.In, 0, 4, true)

--创建无限循环的TweenInfo(RepeatCount小于0表示无限循环)
local infinite = TweenInfo.New(0.5, Enum.EasingStyle.Back, Enum.EasingDirection.In, 0, -1, true)

--创建带延迟的TweenInfo(每次循环之间有1秒延迟)
local delayed = TweenInfo.New(0.5, Enum.EasingStyle.Back, Enum.EasingDirection.In, 1, 4, true)

--使用TweenInfo创建补间动画
local goal = {}
goal.Position = Vector2.new(300, 200)
goal.Size = Vector2.new(200, 100)

local tween = TweenService:Create(button, timeChanged, goal)
tween:Play()

无限循环动画示例:

lua
local TweenService = game:GetService("TweenService")

local part = SandboxNode.new("Part", game.WorkSpace)
part.Position = Vector3.new(0, 10, 0)

--创建无限循环的TweenInfo(持续2秒,线性缓动,无限循环,带反转)
local tweenInfo = TweenInfo.New(
    2, -- Time:动画持续时间2秒
    Enum.EasingStyle.Linear, -- EasingStyle:线性缓动
    Enum.EasingDirection.Out, -- EasingDirection:向后应用
    0, -- DelayTime:无延迟
    -1, -- RepeatCount:小于0表示无限循环
    true -- Reverses:完成目标后会反转
)

local tween = TweenService:Create(part, tweenInfo, {Position = Vector3.new(0, 30, 0)})
tween:Play()

--10秒后取消动画
wait(10)
tween:Cancel()