HarmonyOS Tabs组件滑动到边缘的时候如何禁用滑动效果?现在滑动到边缘了,还能继续画出来一段距离?

如题:HarmonyOS Tabs组件滑动到边缘的时候如何禁用滑动效果?现在滑动到边缘了,还能继续画出来一段距离?

阅读 529
1 个回答

UX标准所有滑动都应有过界回弹效果,tabs组件边缘滑动效果是符合UX标准的。禁止滑动的话,解决方案可以给边缘tabContent添加手势。

示例:

struct TabPage1 {
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()
  private panOption1: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right })
  private panOption2: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left })

  @Builder tabBuilder(index: number, name: string) {
    Column() {
      Text(name)
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(16)
        .fontWeight(this.currentIndex === index ? 500 : 400)
        .lineHeight(22)
        .margin({ top: 17, bottom: 7 })
      Divider()
        .strokeWidth(2)
        .color('#007DFF')
        .opacity(this.currentIndex === index ? 1 : 0)
    }.width('100%')
  }

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#00CB87')
        }.tabBar(this.tabBuilder(0, 'green'))
        // 左右拖动触发该手势事件
        .gesture(
          PanGesture(this.panOption1)
            .onActionStart((event?: GestureEvent) => {
              console.info('Pan start')
            })
            .onActionUpdate((event?: GestureEvent) => {
              if (event) {
              }
            })
            .onActionEnd(() => {
              console.info('Pan end')
            })
        )

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#007DFF')
        }.tabBar(this.tabBuilder(1, 'blue'))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#FFBF00')
        }.tabBar(this.tabBuilder(2, 'yellow'))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#E67C92')
        }.tabBar(this.tabBuilder(3, 'pink'))

        // 左右拖动触发该手势事件

        .gesture(
          PanGesture(this.panOption2)
            .onActionStart((event?: GestureEvent) => {
              console.info('Pan start')
            })
            .onActionUpdate((event?: GestureEvent) => {
              if (event) {
              }
            })
            .onActionEnd(() => {
              console.info('Pan end')
            })
        )
      }

      .vertical(false)
      .barMode(BarMode.Fixed)
      .barWidth(360)
      .barHeight(56)
      .animationDuration(400)
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .width(360)
      .height(296)
      .margin({ top: 52 })
      .backgroundColor('#F1F3F5')
    }.width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进