#鸿蒙通关秘籍#如何在TabBar图标点击后实现动画效果

HarmonyOS
2024-12-06 15:57:43
783浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
雪域狼NLP

在TabBar图标点击后实现动画效果,可以通过offset属性和animation属性来控制图标的纵向偏移及过渡动画。为此,首先定义一个selectedIndex变量用于存储当前被选中的TabBar下标。在点击时更新该变量,以此控制图标显示及动画效果。以下代码展示了实现步骤:

Column() {
   Image(this.selectedIndex === this.tabBarIndex ? TABINFO[this.tabBarIndex].selectedIcon :
         TABINFO[this.tabBarIndex].defaultIcon)
     .interpolation(ImageInterpolation.High)
     .size(this.selectedIndex === HOME_TAB_BAR_INDEX && this.selectedIndex === this.tabBarIndex ?
       { 
         width: $r('app.integer.custom_tab_community_image_size'), 
         height: $r('app.integer.custom_tab_community_image_size') 
       } :
       {
         width: $r('app.integer.custom_tab_image_size'),
         height: $r('app.integer.custom_tab_image_size')
       })
     .offset({
       y: (this.selectedIndex === this.tabBarIndex && this.selectedIndex !== COMMUNITY_TAB_BAR_INDEX) ?
       this.iconOffset : $r('app.integer.custom_tab_common_size_0')
     })
     .animation({
       duration: 400,
       curve: Curve.Ease,
       iterations: 1,
       playMode: PlayMode.Normal
     })
  }
  .width(this.selectedIndex === HOME_TAB_BAR_INDEX && this.selectedIndex === this.tabBarIndex ?
         $r('app.integer.custom_tab_community_image_size') : $r('app.integer.custom_tab_image_container_size'))
  .height(this.selectedIndex === HOME_TAB_BAR_INDEX && this.selectedIndex === this.tabBarIndex ?
         $r('app.integer.custom_tab_community_image_size') : $r('app.integer.custom_tab_image_container_size'))
  .justifyContent(FlexAlign.Center)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
分享
微博
QQ
微信
回复
2024-12-06 17:25:51
相关问题