鸿蒙OS开发之动画相干示例分享, 关于弹出倒计时动画的实战案例源码分享 ...

打印 上一主题 下一主题

主题 875|帖子 875|积分 2625

底子动画案例

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State
  5.   btnWidth:number = 200 // 按钮的宽度
  6.   @State
  7.   btnHeight:number = 100 // 按钮的高度
  8.   build() {
  9.    Row(){
  10.      Column(){
  11.        Button("测试")
  12.          .width(this.btnWidth)
  13.          .height(this.btnHeight)
  14.        // 按钮: 用来启动动画
  15.        Button("动画开始")
  16.          .onClick(()=>{
  17.            // 写动画
  18.            animateTo({duration:1000},()=>{
  19.              this.btnWidth=100
  20.              this.btnHeight=50
  21.            })
  22.          })
  23.      }.width("100%")
  24.    }.height("100%")
  25.   }
  26. }
复制代码
动画的播放次数

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State
  5.   btnWidth:number = 200 // 按钮的宽度
  6.   @State
  7.   btnHeight:number = 100 // 按钮的高度
  8.   build() {
  9.    Row(){
  10.      Column(){
  11.        Button("测试")
  12.          .width(this.btnWidth)
  13.          .height(this.btnHeight)
  14.        // 按钮: 用来启动动画
  15.        Button("动画开始")
  16.          .onClick(()=>{
  17.            // 写动画
  18.            // iterations: -1 表示永久, 其他表示固定次数
  19.            animateTo({duration:1000, iterations: 3},()=>{
  20.              this.btnWidth=100
  21.              this.btnHeight=50
  22.            })
  23.          })
  24.      }.width("100%")
  25.    }.height("100%")
  26.   }
  27. }
复制代码
动画的播放模式

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State
  5.   btnWidth:number = 200 // 按钮的宽度
  6.   @State
  7.   btnHeight:number = 100 // 按钮的高度
  8.   build() {
  9.    Row(){
  10.      Column(){
  11.        Button("测试")
  12.          .width(this.btnWidth)
  13.          .height(this.btnHeight)
  14.        // 按钮: 用来启动动画
  15.        Button("动画开始")
  16.          .onClick(()=>{
  17.            // 写动画
  18.            // iterations: -1 表示永久, 其他表示固定次数
  19.            // playMode: Reverse 反向 Alternate反复
  20.            animateTo({duration:1000, iterations: -1, playMode:PlayMode.Alternate},()=>{
  21.              this.btnWidth=100
  22.              this.btnHeight=50
  23.            })
  24.          })
  25.      }.width("100%")
  26.    }.height("100%")
  27.   }
  28. }
复制代码
animation动画

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State
  5.   btnWidth:number = 200 // 按钮的宽度
  6.   @State
  7.   btnHeight:number = 100 // 按钮的高度
  8.   build() {
  9.    Row(){
  10.      Column(){
  11.        Button("测试")
  12.          .width(this.btnWidth)
  13.          .height(this.btnHeight)
  14.          .animation({
  15.            duration:1000,
  16.            iterations: -1,
  17.            playMode:PlayMode.Alternate
  18.          })
  19.        // 按钮: 用来启动动画
  20.        Button("动画开始")
  21.          .onClick(()=>{
  22.              this.btnWidth=100
  23.              this.btnHeight=50
  24.          })
  25.      }.width("100%")
  26.    }.height("100%")
  27.   }
  28. }
复制代码
scale缩放动画

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State
  5.   btnWidth:number = 200 // 按钮的宽度
  6.   @State
  7.   btnHeight:number = 100 // 按钮的高度
  8.   @State
  9.   btnScale:number = 1; // 缩放
  10.   build() {
  11.    Row(){
  12.      Column(){
  13.        Button("测试")
  14.          .width(this.btnWidth)
  15.          .height(this.btnHeight)
  16.          .scale({
  17.            x: this.btnScale,
  18.            y: this.btnScale,
  19.          })
  20.          .animation({
  21.            duration:1000,
  22.            iterations: -1,
  23.            playMode:PlayMode.Alternate
  24.          })
  25.        // 按钮: 用来启动动画
  26.        Button("动画开始")
  27.          .onClick(()=>{
  28.              this.btnScale *= 2
  29.          })
  30.      }.width("100%")
  31.    }.height("100%")
  32.   }
  33. }
复制代码
显示隐蔽动画

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State
  5.   message:string = "你好, 张大鹏!"
  6.   @State
  7.   isShowMessage:boolean = true
  8.   build() {
  9.    Row(){
  10.      Column(){
  11.        // 固定高度的Column
  12.        Column(){
  13.          if(this.isShowMessage){
  14.            Text(this.message)
  15.              .fontSize(50)
  16.              .fontWeight(FontWeight.Bold)
  17.          }
  18.        }.height(50)
  19.        // 按钮, 控制
  20.        Button("显示/隐藏")
  21.          .onClick(()=>{
  22.            animateTo({duration:1000},()=>{
  23.              this.isShowMessage=!this.isShowMessage
  24.            })
  25.          })
  26.      }.width("100%")
  27.    }.height("100%")
  28.   }
  29. }
复制代码
弹出模态框

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State
  5.   isShowDialog:boolean = false
  6.   // 模态框内容
  7.   @Builder
  8.   getContent(){
  9.     Column(){
  10.       Text("测试")
  11.         .fontColor(Color.White)
  12.     }
  13.     .justifyContent(FlexAlign.Center)
  14.     .backgroundColor(Color.Blue)
  15.     .width("100%")
  16.     .height("100%")
  17.   }
  18.   build() {
  19.    Row(){
  20.      Button("显示/隐藏")
  21.        .onClick(()=>{
  22.          this.isShowDialog=!this.isShowDialog
  23.        })
  24.    }.height("100%")
  25.     .bindContentCover(
  26.       $$this.isShowDialog, // 模态框弹出条件
  27.       this.getContent, // 绑定模态框
  28.     )
  29.   }
  30. }
复制代码
弹出倒计时广告

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State
  5.   isShowDialog: boolean = false
  6.   @State
  7.   timerCount: number = 5 // 默认5秒倒计时关闭广告
  8.   timer: number = -1 // 定时器
  9.   // 开始倒计时
  10.   beginTimerCount() {
  11.     this.timer = setInterval(() => {
  12.       if (this.timerCount === 0) {
  13.         clearInterval(this.timer)
  14.         this.timerCount = 5 // 重置计时器
  15.         this.isShowDialog = false // 关闭模态框
  16.       }
  17.       this.timerCount--
  18.     }, 1000)
  19.   }
  20.   // 生命周期方法: 页面消失之前
  21.   aboutToDisappear(): void {
  22.     clearInterval(this.timer) // 防止定时器没有及时清理
  23.   }
  24.   // 模态框内容
  25.   @Builder
  26.   getContent() {
  27.     Column() {
  28.       // 右上角
  29.       Row() {
  30.         Text(`还剩${this.timerCount}秒`)
  31.           .fontColor(Color.White)
  32.       }
  33.       .width("100%")
  34.       .justifyContent(FlexAlign.End)
  35.       .padding(10)
  36.     }
  37.     .backgroundColor(Color.Blue)
  38.     .width("100%")
  39.     .height("100%")
  40.   }
  41.   build() {
  42.     Row() {
  43.       Button("显示")
  44.         .onClick(() => {
  45.           this.isShowDialog = true // 显示模态框
  46.           this.beginTimerCount() // 开始倒计时
  47.         })
  48.     }.height("100%")
  49.     .bindContentCover(
  50.       $$this.isShowDialog, // 模态框弹出条件
  51.       this.getContent, // 绑定模态框
  52.       {
  53.         modalTransition: ModalTransition.NONE, // 取消模态框动画
  54.       }
  55.     )
  56.   }
  57. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农妇山泉一亩田

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表