跳到主要内容
版本:Next

LayoutAnimation

当下一次布局发生时,自动将视图动画到其新位置。

在函数组件中,通常会在更新状态 Hook 之前调用此 API;在类组件中,则会在调用 setState 之前调用它。

请注意,要在 Android 上正常使用此功能,需要通过 UIManager 设置以下标志:

JavaScript
if (Platform.OS === 'android') {
if (UIManager.setLayoutAnimationEnabledExperimental) {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
}

示例


参考

方法

configureNext()

React TSX
static configureNext(
config: LayoutAnimationConfig,
onAnimationDidEnd?: () => void,
onAnimationDidFail?: () => void,
);

安排一个动画在下一次布局时发生。

参数:

名称类型必填描述
configobject参见下面的配置说明。
onAnimationDidEndfunction动画完成时调用。
onAnimationDidFailfunction动画失败时调用。

config 参数是一个包含以下键的对象。create 会返回一个可用于 config 的有效对象,Presets 对象也都可以作为 config 传入。

  • duration,单位为毫秒
  • create,用于为新视图添加动画的可选配置
  • update,用于为已更新视图添加动画的可选配置
  • delete,用于在视图被移除时为其添加动画的可选配置

传给 createupdatedelete 的配置包含以下键:

  • type,要使用的 动画类型
  • property,要动画的 布局属性(可选,但建议用于 createdelete
  • springDamping(数字,可选,仅在 type: Type.spring 时使用)
  • initialVelocity(数字,可选)
  • delay(数字,可选)
  • duration(数字,可选)

create()

React TSX
static create(duration, type, creationProp)

辅助方法,用于创建一个对象(包含 createupdatedelete 字段),以传入 configureNexttype 参数是一个 动画类型creationProp 参数是一个 布局属性

示例:

属性

类型

create 方法中,或在 configureNextcreate/update/delete 配置中使用的动画类型枚举。(示例用法:LayoutAnimation.Types.easeIn

Types
spring
linear
easeInEaseOut
easeIn
easeOut
keyboard

属性

create 方法中,或在 configureNextcreate/update/delete 配置中使用的布局属性枚举。(示例用法:LayoutAnimation.Properties.opacity

Properties
opacity
scaleX
scaleY
scaleXY

预设

一组预定义的动画配置,可传入 configureNext

PresetsValue
easeInEaseOutcreate(300, 'easeInEaseOut', 'opacity')
linearcreate(500, 'linear', 'opacity')
spring{duration: 700, create: {type: 'linear', property: 'opacity'}, update: {type: 'spring', springDamping: 0.4}, delete: {type: 'linear', property: 'opacity'} }

easeInEaseOut

调用 configureNext(),使用 Presets.easeInEaseOut


linear

调用 configureNext(),使用 Presets.linear


spring

调用 configureNext(),使用 Presets.spring

示例: