使用transition属性配置组件内转场动画效果。
示例代码如下:
@Entry
@Component
struct Index {
// 内容显示控制
@State showInfo: boolean = true
// 加载显示控制
@State load: boolean = true
aboutToAppear(): void {
// 模拟初始化加载场景
setTimeout(() => {
this.load = false
}, 2000)
}
@Builder
seen(text: string) {
Stack() {
if (this.load) {
Text("加载中")
.transition(TransitionEffect.OPACITY.animation({ duration: 500 }))
} else {
Image($r('app.media.startIcon'))
.height(500)
.width(500)
.transition(TransitionEffect.OPACITY.animation({ duration: 1000 }))
Text(text)
.fontSize(24)
.fontColor(Color.Pink)
.transition(TransitionEffect.OPACITY.animation({ duration: 1000 }))
}
}
.width('100%')
.height('100%')
.transition(TransitionEffect.OPACITY.animation({ duration: 500 }))
}
build() {
Column() {
Blank()
.height(20)
Row() {
Button("看过")
.onClick(() => {
this.load = true
this.showInfo = true
setTimeout(() => {
this.load = false
}, 2000)
})
Button("追剧")
.onClick(() => {
this.load = true
this.showInfo = false
setTimeout(() => {
this.load = false
}, 2000)
})
}
.width('100%')
.justifyContent(FlexAlign.SpaceAround)
// 模拟组件插入删除
if (this.showInfo) {
this.seen("暂无看过")
} else {
this.seen("暂未追剧")
}
}
}
} Q:常见动画效果有哪些?
A:透明、平移、旋转、缩放等,可以单独使用,也可以组合使用。具体可以参考TransitionEffect对象说明。
Q:如何对出现和消失配置不同的动画效果?
A:可以使用TransitionEffect的asymmetric参数,用于指定非对称转场效果。