跳到主要内容
版本:0.79

LayoutAnimation

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

使用此 API 的一种常见方法是在函数组件中更新状态钩子之前调用它,以及在类组件中调用 setState 之前调用它。

注意,为了让它在 Android 上工作,你需要通过 UIManager 设置以下标志:

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

示例


参考

方法

configureNext()

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()

tsx
static create(duration, type, creationProp)

辅助函数,创建一个对象(包含 createupdatedelete 字段)以传递给 configureNexttype 参数是一个 动画类型creationProp 参数是一个 布局属性

示例:

属性

类型

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

类型
spring
linear
easeInEaseOut
easeIn
easeOut
keyboard

属性

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

属性
opacity
scaleX
scaleY
scaleXY

预设

一组预定义的动画配置,传递给 configureNext

预设
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

使用 Presets.easeInEaseOut 调用 configureNext()


linear

使用 Presets.linear 调用 configureNext()


spring

使用 Presets.spring 调用 configureNext()

示例: