@Entry
@Component
struct FlipDemoPage {
@State flag: boolean = false;
@State rotateAngle: number = 0;
build() {
Column() {
Button('翻转')
.onClick(() => {
this.flag = !this.flag;
this.rotateAngle = 0;
this.getUIContext().animateTo({
duration: 500,
curve: Curve.EaseInOut,
onFinish: () => {
this.rotateAngle = 0;
}
}, () => {
if (this.flag) {
this.rotateAngle = 180;
} else {
this.rotateAngle = -180;
}
})
})
.margin(50)
Stack() {
if (this.flag) {
Text('0000')
.fontColor(Color.Blue)
} else {
Text('1111')
.fontColor(Color.Blue)
}
}.rotate({
x: 1,
y: 0,
z: 0,
angle: this.rotateAngle
})
}
.height('100%')
.width('100%')
}
} 使用组件内转场 (transition)来实现文字翻转动效,在文字显示和消失时增加过渡效果。
@Entry
@Component
struct FlipDemoPage {
@State flag: boolean = false;
build() {
Column() {
Button('翻转')
.onClick(() => {
this.flag = !this.flag;
})
.margin(50)
Stack() {
if (this.flag) {
Text('0000')
.fontColor(Color.Blue)
.transition(TransitionEffect.OPACITY.animation({ duration: 500, curve: Curve.ExtremeDeceleration })
.combine(TransitionEffect.rotate({ x: 1, angle: 180 })))
} else {
Text('1111')
.fontColor(Color.Blue)
.transition(TransitionEffect.OPACITY.animation({ duration: 500, curve: Curve.ExtremeDeceleration })
.combine(TransitionEffect.rotate({ x: 1, angle: 180 })))
}
}
}
.height('100%')
.width('100%')
}
}