背景知识
scrollable:设置是否可以通过滑动页面进行Tab页面切换,默认支持向左向右两个方向滑动。
gesture:通用属性手势绑定,可绑定TapGesture、LongPressGesture、PanGesture、PinchGesture、RotationGesture、SwipeGesture等手势。
解决方案
panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right })
示例代码如下:
@Component
struct TabsPageSwitching {
@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)
.fontSize(16)
.lineHeight(22)
.margin({ top: 16, bottom: 16 })
.fontColor(this.currentIndex === index ? Color.Blue : Color.Black)
Divider()
.strokeWidth(2)
.color(Color.Blue)
.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(Color.Green)
}
.tabBar(this.tabBuilder(0, 'Tab1'))
TabContent() {
Column()
.width('100%')
.height('100%')
.backgroundColor(Color.Pink)
.gesture(PanGesture(this.panOption))
}
.tabBar(this.tabBuilder(1, 'Tab2'))
TabContent() {
Column()
.width('100%')
.height('100%')
.backgroundColor(Color.Orange)
.gesture(PanGesture(this.panOption))
}
.tabBar(this.tabBuilder(2, 'Tab3'))
}
.vertical(false)
.barMode(BarMode.Fixed)
.barWidth(360)
.barHeight(56)
.animationDuration(200)
.onChange((index: number) => {
this.currentIndex = index;
})
.width('100%')
.height(296)
.margin({ top: 52 })
}
.width('100%')
}
}