#鸿蒙通关秘籍#Tabs组件中如何实现自定义页面切换动画?

HarmonyOS
21h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
hm673ff0d790c8e

通过 customContentTransition 接口实现自定义页面切换动画。当使用自定义切换动画时,Tabs组件自带的默认切换动画会被禁用,同时页面也无法跟手滑动。以下是接口的用法:

@Entry
@Component
struct TabsCustomAnimationExample {
  @State data = [
    { text: 'Red', backgroundColor: Color.Red },
    { text: 'Yellow', backgroundColor: Color.Yellow },
    { text: 'Blue', backgroundColor: Color.Blue }
  ];
  @State opacityList = [1.0, 1.0, 1.0];
  @State scaleList = [1.0, 1.0, 1.0];

  private customContentTransition = (from: number, to: number) => {
    return {
      timeout: 1000,
      transition: (proxy) => {
        this.scaleList = [0.5, 0.5, 0.5];
        this.opacityList = [0.5, 0.5, 0.5];
        animateTo({
          duration: 1000,
          onFinish: () => proxy.finishTransition()
        }, () => {
          this.scaleList[from] = 0.5;
          this.scaleList[to] = 1.0;
          this.opacityList[from] = 0.5;
          this.opacityList[to] = 1.0;
        });
      }
    };
  };

  build() {
    Column() {
      Tabs() {
        ForEach(this.data, (item, index) => {
          TabContent() {}
          .tabBar(item.text)
          .backgroundColor(item.backgroundColor)
          .opacity(this.opacityList[index])
          .scale({ x: this.scaleList[index], y: this.scaleList[index] });
        });
      }
      .customContentTransition(this.customContentTransition)
    }
  }
}
分享
微博
QQ
微信
回复
18h前
相关问题
swiper组件如何实现自定义切换动画
661浏览 • 1回复 待解决
如何自定义模拟Tabs组件
846浏览 • 1回复 待解决
如何实现自定义应用入场动画
739浏览 • 1回复 待解决