基础动画案例
@Entry
@Component
struct Index {
@State
btnWidth:number = 200
@State
btnHeight:number = 100
build() {
Row(){
Column(){
Button("测试")
.width(this.btnWidth)
.height(this.btnHeight)
Button("动画开始")
.onClick(()=>{
animateTo({duration:1000},()=>{
this.btnWidth=100
this.btnHeight=50
})
})
}.width("100%")
}.height("100%")
}
}
动画的播放次数
@Entry
@Component
struct Index {
@State
btnWidth:number = 200
@State
btnHeight:number = 100
build() {
Row(){
Column(){
Button("测试")
.width(this.btnWidth)
.height(this.btnHeight)
Button("动画开始")
.onClick(()=>{
animateTo({duration:1000, iterations: 3},()=>{
this.btnWidth=100
this.btnHeight=50
})
})
}.width("100%")
}.height("100%")
}
}
动画的播放模式
@Entry
@Component
struct Index {
@State
btnWidth:number = 200
@State
btnHeight:number = 100
build() {
Row(){
Column(){
Button("测试")
.width(this.btnWidth)
.height(this.btnHeight)
Button("动画开始")
.onClick(()=>{
animateTo({duration:1000, iterations: -1, playMode:PlayMode.Alternate},()=>{
this.btnWidth=100
this.btnHeight=50
})
})
}.width("100%")
}.height("100%")
}
}
animation动画
@Entry
@Component
struct Index {
@State
btnWidth:number = 200
@State
btnHeight:number = 100
build() {
Row(){
Column(){
Button("测试")
.width(this.btnWidth)
.height(this.btnHeight)
.animation({
duration:1000,
iterations: -1,
playMode:PlayMode.Alternate
})
Button("动画开始")
.onClick(()=>{
this.btnWidth=100
this.btnHeight=50
})
}.width("100%")
}.height("100%")
}
}
scale缩放动画
@Entry
@Component
struct Index {
@State
btnWidth:number = 200
@State
btnHeight:number = 100
@State
btnScale:number = 1;
build() {
Row(){
Column(){
Button("测试")
.width(this.btnWidth)
.height(this.btnHeight)
.scale({
x: this.btnScale,
y: this.btnScale,
})
.animation({
duration:1000,
iterations: -1,
playMode:PlayMode.Alternate
})
Button("动画开始")
.onClick(()=>{
this.btnScale *= 2
})
}.width("100%")
}.height("100%")
}
}
显示隐藏动画
@Entry
@Component
struct Index {
@State
message:string = "你好, 张大鹏!"
@State
isShowMessage:boolean = true
build() {
Row(){
Column(){
Column(){
if(this.isShowMessage){
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}.height(50)
Button("显示/隐藏")
.onClick(()=>{
animateTo({duration:1000},()=>{
this.isShowMessage=!this.isShowMessage
})
})
}.width("100%")
}.height("100%")
}
}
弹出模态框
@Entry
@Component
struct Index {
@State
isShowDialog:boolean = false
@Builder
getContent(){
Column(){
Text("测试")
.fontColor(Color.White)
}
.justifyContent(FlexAlign.Center)
.backgroundColor(Color.Blue)
.width("100%")
.height("100%")
}
build() {
Row(){
Button("显示/隐藏")
.onClick(()=>{
this.isShowDialog=!this.isShowDialog
})
}.height("100%")
.bindContentCover(
$$this.isShowDialog,
this.getContent,
)
}
}
弹出倒计时广告
@Entry
@Component
struct Index {
@State
isShowDialog: boolean = false
@State
timerCount: number = 5
timer: number = -1
beginTimerCount() {
this.timer = setInterval(() => {
if (this.timerCount === 0) {
clearInterval(this.timer)
this.timerCount = 5
this.isShowDialog = false
}
this.timerCount--
}, 1000)
}
aboutToDisappear(): void {
clearInterval(this.timer)
}
@Builder
getContent() {
Column() {
Row() {
Text(`还剩${this.timerCount}秒`)
.fontColor(Color.White)
}
.width("100%")
.justifyContent(FlexAlign.End)
.padding(10)
}
.backgroundColor(Color.Blue)
.width("100%")
.height("100%")
}
build() {
Row() {
Button("显示")
.onClick(() => {
this.isShowDialog = true
this.beginTimerCount()
})
}.height("100%")
.bindContentCover(
$$this.isShowDialog,
this.getContent,
{
modalTransition: ModalTransition.NONE,
}
)
}
}