目录

如何设置TabContent切换动画

问题现象

场景一:TabContent切换如何实现渐隐渐显的动画效果?

场景二:点击页签,TabContent有左右切换效果,A、B、C三个页面,B页面居中,左滑A宽80%拼接B左侧20%,右滑同理。

背景知识

解决方案

场景一:实现渐隐渐显的动画效果。

  1. 监听页面手势滑动,计算滑动进度,实时修改页面的透明度、缩放等属性,实现拖拽动效。
  2. 滑动结束后,重置页面样式,完成释放自动回弹还原的平滑过渡。
  3. 点击页签通过onTabBarClick执行新旧页面切换动效。
参考代码如下所示:
@Entry
@Component
struct FadePage {
  private durationList: number[] = [];
  private barList: string[] = ['#33000000', '#1a000000', '#0d000000'];
  private isRestAnim: boolean = false; // 滑动回弹动画是否正在执行
  @State opacityList: number[] = [];
  @State scaleList: number[] = [];
  @State currentIndex: number = 0;

  // 点击Tab渐显渐隐
  private clickTabHandle(from: number, to: number) {
    // 页面重复点击,直接拦截
    if (from === to) {
      return;
    }
    // 如果回弹动画正在进行:强制终止、重置
    if (this.isRestAnim) {
      // 清空滑动残留的中间插值状态
      for (let i = 0; i < this.barList.length; i++) {
        this.scaleList[i] = i === from ? 1 : 0.9;
        this.opacityList[i] = i === from ? 1 : 0;
      }
      // 强制关闭回弹动画
      this.isRestAnim = false;
    }
    // 新页面初始状态
    this.opacityList[to] = 0;
    this.scaleList[to] = 0.9;
    this.getUIContext().animateTo({ duration: this.durationList[from], curve: Curve.EaseOut }, () => {
      // 原页面:透明淡出+缩小
      this.opacityList[from] = 0;
      this.scaleList[from] = 0.9;
      // 目标页面:透明淡入+放大
      this.opacityList[to] = 1;
      this.scaleList[to] = 1;
      this.currentIndex = to;
    });
  }

  // 滑动跟手渐显渐隐
  private gestureSwiperHandle(curIndex: number, event: TabsAnimationEvent) {
    let offset = event.currentOffset;
    // 判断左右滑动
    let targetIndex = offset > 0 ? curIndex - 1 : curIndex + 1;
    if (targetIndex < 0 || targetIndex >= this.opacityList.length) {
      return;
    }
    // 跟手进度计算
    let progress = Math.min(Math.abs(offset) / 300, 1);
    // 当前页面
    this.opacityList[curIndex] = 1 - progress;
    this.scaleList[curIndex] = 1 - progress * 0.1;
    // 目标页面
    this.opacityList[targetIndex] = progress;
    this.scaleList[targetIndex] = 0.9 + progress * 0.1;
  }

  // 滑动回弹复原
  private resetStyle(from: number, to: number) {
    // 开启回弹动画
    this.isRestAnim = true;
    this.getUIContext().animateTo({
      duration: this.durationList[from], curve: Curve.EaseOut, onFinish: () => {
        // 动画结束时关闭回弹动画
        this.isRestAnim = false;
      }
    }, () => {
      for (let i = 0; i < this.barList.length; i++) {
        this.scaleList[i] = i === to ? 1 : 0.9;
        this.opacityList[i] = i === to ? 1 : 0;
      }
    });
  }

  aboutToAppear(): void {
    let duration = 1000;
    for (let i = 1; i <= this.barList.length; i++) {
      this.opacityList.push(1.0);
      this.scaleList.push(1.0);
      this.durationList.push(duration * i);
    }
  }

  build() {
    Column() {
      Tabs() {
        ForEach(this.barList, (item: string, index: number) => {
          TabContent()
            .tabBar(`页签${index + 1}`)
            .backgroundColor(item)
            // 自定义动画变化透明度、缩放页面等
            .opacity(this.opacityList[index])
            .scale({ x: this.scaleList[index], y: this.scaleList[index] });
        });
      }
      .scrollable(true)
      .onAnimationEnd((index: number) => {
        this.resetStyle(this.currentIndex, index);
        this.currentIndex = index;
      })
      .onGestureSwipe((index: number, event: TabsAnimationEvent) => {
        this.gestureSwiperHandle(index, event);
      })
      .onTabBarClick((toIndex: number) => {
        let fromIndex = this.currentIndex;
        this.clickTabHandle(fromIndex, toIndex);
      });
    }
    .width('100%')
    .height('100%');
  }
}

效果图如下所示:

常见FAQ

Q:AnimationMode.NO_ANIMATION关闭默认动画不起作用,如何解决?

A:可通过设置animationDuration为0。