HarmonyOS Tabs如何实现允许左滑但是禁止右滑?

Tabs如何实现允许左滑但是禁止右滑,swiper无法使用,因为需要手动切换到指定index,swiper只能上一页下一页的切换。

HarmonyOS
2024-10-17 11:07:35
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可以使用PanGesture手势事件来实现,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-gestures-pangesture-V5#pangestureoptions

想禁止右滑动的划,可以只禁掉往右,往左不做操作,是不影响原来的动画效果。

可以参考demo:

@Entry  
@Component  
struct TabsExample {  
  @State fontColor: string = '#182431'  
  @State selectedFontColor: string = '#007DFF'  
  @State currentIndex: number = 0  
  private controller: TabsController = new TabsController()  
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right })  
  @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, index: this.currentIndex, controller: this.controller }) {  
        TabContent() {  
          Column().width('100%').height('100%').backgroundColor('#ffb554d7')  
        }.tabBar(this.tabBuilder(0, 'index'))  
  
        TabContent() {  
          Column().width('100%').height('100%').backgroundColor(Color.Pink).gesture(PanGesture(this.panOption))  
        }.tabBar(this.tabBuilder(1, 'detail'))  
  
        TabContent() {  
          Column().width('100%').height('100%').backgroundColor('#ffc19757').gesture(PanGesture(this.panOption))  
        }.tabBar(this.tabBuilder(2, 'me'))  
  
      }  
      .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%')  
  }  
}
panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right })
  • 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.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.

这个的意思是对往右滑的手势做监听,如果没有手动去写他的onActionStart onActionEnd方法,会表现出 " 禁用 "的效果。所以说如果要左右都可以滑动,可以设置为None。

分享
微博
QQ
微信
回复
2024-10-17 18:00:13


相关问题
HarmonyOS 怎么禁止
527浏览 • 1回复 待解决
HarmonyOS 页面禁止
924浏览 • 1回复 待解决
【JS】如何实现删除功能?
4277浏览 • 1回复 待解决
HarmonyOS 怎么禁止
749浏览 • 1回复 待解决
HarmonyOS 应用内负一屏如何实现
1145浏览 • 1回复 待解决
HarmonyOS dialog如何禁止返回关闭
648浏览 • 1回复 待解决
HarmonyOS list 字体显示异常
593浏览 • 1回复 待解决
智能穿戴开发怎么关闭退出?
4564浏览 • 1回复 待解决
HarmonyOS 如何禁止系统的侧返回
473浏览 • 1回复 待解决
获取返回手势方法
1200浏览 • 1回复 待解决
轻量级智能穿戴退出过于灵敏
3549浏览 • 1回复 待解决
HarmonyOS 二级页面关闭问题
1033浏览 • 1回复 待解决