类型 函数 返回值 补间动画 (Tween) 修订版本 版本 2024.3703 关键词 动画, 补间动画, 时间轴, 插值, to 另请参阅 动画 — 补间动画和时间轴 (指南) animation.from() 补间动画 (Tween) 时间轴 (Timeline)
此函数使用可选的 缓动 算法对对象的属性进行动画处理(插值)。它返回一个对应于对象 target
插值的 补间动画 (Tween) 引用。
animation.to( target, properties, params )
显示对象 (DisplayObject) 或 用户数据 (Userdata)。 要进行补间动画的显示对象、矩形路径 (RectPath) 点、绘制 (Paint) 填充或 fill.effect。
表 (Table)。 指定将进行插值的对象属性的表。这些属性包括以下选项:
x1
、y1
、x2
、y2
、x3
、y3
、x4
和 y4
。有关更多信息,请参见此处。r
、g
、b
和 a
).fill
表时适用。表 (Table)。 指定补间动画控制参数的表,有关详细信息,请参阅下一节。
params
表包含控制补间动画方面的参数。
数字 (Number)。 指定补间动画的持续时间(以毫秒为单位)。默认为 500
。
数字 (Number)。 调整补间动画的相对速度比例。该值必须是大于 0
的正数。默认情况下,速度比例为 1
(正常速度)。大于 1
的值将增加速度,而小于 1
的值将降低速度。
函数 (Function)。 指定 缓动 算法。默认为 easing.linear
。
数字 (Number)。 指定补间动画开始前的延迟时间(以毫秒为单位)。默认为 0
。
数字 (Number)。 指定补间动画重复的次数。默认情况下,迭代值为 1
(不重复)。要使补间动画无限重复,请为此参数指定 0
或 -1
。
布尔值 (Boolean)。 指定补间动画的交替迭代是否反映(反向播放)。默认为 false
。
字符串 (String)。 指定补间动画标签。动画库可以暂停、恢复、取消、设置位置或设置共享相同标签的补间动画的速度比例。
字符串 (String)。 要分配给补间动画的可选标识字符串。可以从任何补间动画事件侦听器函数(如下所示)中以 obj.id
的形式检索此字符串。
布尔值 (Boolean)。 指定非控制参数是解释为最终结束值还是值的变化。默认为 false
。
数字 (Number)。 指定constantRateProperty
参数以指示哪个对象状态控制插值。这两个参数一起可用于以恒定速率对多个对象进行插值,即使它们不共享相同的初始/最终位置、旋转、alpha 或比例。请注意,如果提供此参数,则将忽略 time
参数,因为
字符串 (String)。 指示哪个对象状态控制"position"
、"rotation"
、"alpha"
或 "scale"
。如果提供 constantRate
参数,则 **必须** 指定此参数。
侦听器 (Listener)。 在补间动画开始之前调用的侦听器函数。此函数将接收对关联的 补间动画 (Tween) 的引用作为其唯一参数。
侦听器 (Listener)。 在补间动画完成后调用的侦听器函数。此函数将接收对关联的 补间动画 (Tween) 的引用作为其唯一参数。
侦听器 (Listener)。 暂停补间动画时调用的侦听器函数。此函数将接收对关联的 补间动画 (Tween) 的引用作为其唯一参数。
侦听器 (Listener)。 恢复补间动画时调用的侦听器函数。此函数将接收对关联的 补间动画 (Tween) 的引用作为其唯一参数。
侦听器 (Listener)。 取消补间动画时调用的侦听器函数。此函数将接收对关联的 补间动画 (Tween) 的引用作为其唯一参数。
侦听器 (Listener)。 当补间动画在重复周期中完成一次迭代时调用的侦听器函数。此函数将接收对关联的 补间动画 (Tween) 的引用作为其唯一参数。
侦听器 (Listener)。 当通过 animation.setPosition() 或 object:setPosition() 手动更改补间动画的播放位置时调用的侦听器函数。
local square = display.newRect( 0, 0, 100, 100 ) local w, h = display.contentWidth, display.contentHeight local function tweenListener( obj ) print( "Tween completed; ID: " .. obj.id ) end -- Move square to bottom right corner animation.to( square, { x=(w-50), y=(h-50) }, { time=1500, id="tween1", onComplete=tweenListener } )
local square1 = display.newRect( 50, 50, 100, 100 ) square1:setFillColor( 1, 0, 0 ) local square2 = display.newRect( 50, 150, 100, 100 ) square2:setFillColor( 0, 1, 0 ) local square3 = display.newRect( 50, 250, 100, 100 ) square3:setFillColor( 0, 0, 1 ) local w, h = display.contentWidth, display.contentHeight local cr = 50 -- Set a constant rate for movement (pixels per second) -- Move each square at a constant rate to its final position animation.to( square1, { x=(w-50) }, { constantRate=cr, constantRateProperty="position" } ) animation.to( square2, { x=(w-150) }, { constantRate=cr, constantRateProperty="position" } ) animation.to( square3, { x=(w-100) }, { constantRate=cr, constantRateProperty="position" } )
local square = display.newRect( 0, 0, 100, 100 ) -- Tween the path corner points of the square -- Notice that the target being tweened is "square.path", not "square" animation.to( square.path, { x1=10, y1=10, x2=20, y2=-20, x3=-10, y3=-10, x4=-40, y4=40 }, { time=1500 } )
local logo = display.newImageRect( "logo.png", 100, 100 ) -- Apply a hue filter effect to the image logo.fill.effect = "filter.hue" -- Tween the hue filter effect's "angle" property -- Notice that the target being tweened is "logo.fill.effect", not "logo" animation.to( logo.fill.effect, { angle=100 }, { time=1500 } )
local square = display.newRect( 0, 0, 100, 100 ) -- Tween specific RGB+A color channels of the square -- Notice that the target being tweened is "square.fill", not "square" animation.to( square.fill, { r=0, g=0.2, b=0.6, a=0.8 }, { time=1500 } )