ts-transition-animation-component.md 3.6 KB
Newer Older
Z
zengyawen 已提交
1
# 组件内转场
Z
zengyawen 已提交
2

H
geshi  
HelloCrease 已提交
3
>  **说明:**
Z
zengyawen 已提交
4
> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
5 6


Z
zengyawen 已提交
7
组件转场主要通过transition属性进行配置转场参数,在组件插入和删除时进行过渡动效,主要用于容器组件子组件插入删除时提升用户体验(需要配合animateTo才能生效,动效时长、曲线、延时跟随animateTo中的配置)。
Z
zengyawen 已提交
8 9


Z
zengyawen 已提交
10
## 属性
Z
zengyawen 已提交
11

Z
zengyawen 已提交
12 13 14
| 名称 | 参数类型 | 默认值 | 参数描述 |
| -------- | -------- | -------- | -------- |
| transition | Object | - | 所有参数均为可选参数,详细描述见transition入参说明。 |
Z
zengyawen 已提交
15

Z
zengyawen 已提交
16 17 18
- transition入参说明
  | 参数名称 | 参数类型 | 默认值 | 必填 | 参数描述 |
  | -------- | -------- | -------- | -------- | -------- |
H
geshi  
HelloCrease 已提交
19
  | type | TransitionType                                               | TransitionType.All | 否 | 默认包括组件新增和删除。<br/>>&nbsp;&nbsp;**说明:**<br/>>&nbsp;不指定Type时说明插入删除使用同一种效果。 |
Z
zengyawen 已提交
20 21
  | opacity | number | 1 | 否 | 设置组件转场时的透明度效果,为插入时起点和删除时终点的值。 |
  | translate | {<br/>x?&nbsp;:&nbsp;number,<br/>y?&nbsp;:&nbsp;number,<br/>z?&nbsp;:&nbsp;number<br/>} | - | 否 | 设置组件转场时的平移效果,为插入时起点和删除时终点的值。 |
G
gmy 已提交
22 23
  | scale | {<br/>x?&nbsp;:&nbsp;number,<br/>y?&nbsp;:&nbsp;number,<br/>z?&nbsp;:&nbsp;number,<br/>centerX?&nbsp;:&nbsp;number,<br/>centerY?&nbsp;:&nbsp;number<br/>} | - | 否 | 设置组件转场时的缩放效果,为插入时起点和删除时终点的值。<br/>**说明:**<br/>>&nbsp;中心点为0时,默认的是组件的左上角<br/>>&nbsp;centerX、centerY设置百分比时,是基于自身布局的尺寸缩放的 |
  | rotate | {<br/>x?:&nbsp;number,<br/>y?:&nbsp;number,<br/>z?:&nbsp;number,<br/>angle?:&nbsp;Angle,<br/>centerX?:&nbsp;Length,<br/>centerY?:&nbsp;Length<br/>} | - | 否 | 设置组件转场时的旋转效果,为插入时起点和删除时终点的值。<br/>**说明:**<br/>>&nbsp;中心点为0时,默认的是组件的左上角 |
Z
zengyawen 已提交
24

Z
zengyawen 已提交
25
- TransitionType枚举说明
G
gmy 已提交
26
  | 名称 | 描述 |
Z
zengyawen 已提交
27
  | -------- | -------- |
G
gmy 已提交
28 29 30
  | All | 指定当前的Transition动效生效在组件的所有变化场景。 |
  | Insert | 指定当前的Transition动效生效在组件的插入场景。 |
  | Delete | 指定当前的Transition动效生效在组件的删除场景。 |
Z
zengyawen 已提交
31 32


Z
zengyawen 已提交
33
## 示例
Z
zengyawen 已提交
34 35 36

示例功能通过一个Button控制第二个Button的出现和消失,并通过transition配置第二个Button出现和消失的过场动画。

H
geshi  
HelloCrease 已提交
37 38
```ts
// xxx.ets
Z
zengyawen 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
@Entry
@Component
struct TransitionExample {
  @State btn1: boolean = false
  @State show: string = "show"
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center,}) {
      Button(this.show).width(80).height(30).backgroundColor(0x317aff).margin({bottom:50})
        .onClick(() => {
          animateTo({ duration: 1000 }, () => {
            this.btn1 = !this.btn1
            if(this.btn1){
              this.show = "hide"
            }else{
              this.show = "show"
            }
          })
        })
      if (this.btn1) {
        // 插入和删除配置为不同的过渡效果
        Button() {
          Image($r('app.media.bg1')).width("80%").height(300)
        }.transition({ type: TransitionType.Insert, scale : {x:0,y:1.0} })
        .transition({ type: TransitionType.Delete, scale: { x: 1.0, y: 0.0 } })
      }
    }.height(400).width("100%").padding({top:100})
  }
}
```

Z
zengyawen 已提交
69
![zh-cn_image_0000001174582850](figures/zh-cn_image_0000001174582850.gif)