
回复
项目中Tabs的使用可以说是特别的频繁,但是官方提供的Tabs使用起来,存在tab选项卡切换动画滞后的问题。
0900086000300134184.20201216095126.86523331460016843504112994983392.png
原始动画无法满足产品的UI需求,因此,这篇文章将实现下面页面滑动,tab选项卡实时滑动的动画效果。
0900086000300134184.20201216095126.86523331460016843504112994983392.png
Row() {
Text(name)
.fontSize(16)
.fontWeight(this.SelectedTabIndex == index ? FontWeight.Bold : FontWeight.Normal)
.textAlign(TextAlign.Center)
.animation({ duration: 300 })
Image($r('app.media.send'))
.width(14)
.height(14)
.margin({ left: 2 })
.visibility(this.SelectedTabIndex == index ? Visibility.Visible : Visibility.None)
.animation({ duration: 300 })
}
.justifyContent(FlexAlign.Center)
.layoutWeight(this.SelectedTabIndex == index ? 1.5 : 1)
.animation({ duration: 300 })
Stack() {
Rect()
.height(30)
.stroke(Color.Black)
.radius(10)
.width(this.FirstWidth)
.fill("#bff9f2")
.position({
left: this.IndicatorLeftOffset + this.IndicatorOffset,
bottom: 0
})
.animation({ duration: 300, curve: Curve.LinearOutSlowIn })
}
.width("100%")
.alignRules({
center: { anchor: "Tabs", align: VerticalAlign.Center }
})
Swiper(this.SwiperController) {
ForEach(this.TabNames, (name: string, index: number) => {
Column() {
Text(`${name} - ${index}`)
.fontSize(24)
.fontWeight(FontWeight.Bold)
}
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.height("100%")
.width("100%")
})
}
.onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
if (targetIndex > index) {
this.IndicatorLeftOffset += this.OtherWidth;
} else if (targetIndex < index) {
this.IndicatorLeftOffset -= this.OtherWidth;
}
this.IndicatorOffset = 0
this.SelectedTabIndex = targetIndex
})
.onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
this.IndicatorOffset = 0
})
.onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
let move: number = this.GetOffset(extraInfo.currentOffset);
//这里需要限制边缘情况
if ((this.SelectedTabIndex == 0 && extraInfo.currentOffset > 0) ||
(this.SelectedTabIndex == this.TabNames.length - 1 && extraInfo.currentOffset < 0)) {
return;
}
this.IndicatorOffset = extraInfo.currentOffset < 0 ? move : -move;
})
.onAreaChange((oldValue: Area, newValue: Area) => {
let width = newValue.width.valueOf() as number;
this.SwiperWidth = width;
})
.curve(Curve.LinearOutSlowIn)
.loop(false)
.indicator(false)
.width("100%")
.id("MainContext")
.alignRules({
top: { anchor: "Tabs", align: VerticalAlign.Bottom },
bottom: { anchor: "__container__", align: VerticalAlign.Bottom }
})
@Entry
@ComponentV2
struct Index {
/**
* 标头名称集合
*/
@Local TabNames: string[] = ["飞机", "铁路", "自驾", "地铁", "公交", "骑行"]
/**
* Tab选择索引
*/
@Local SelectedTabIndex: number = 0
/**
* 标点移动距离
*/
@Local IndicatorLeftOffset: number = 0
/**
* 标点在swiper的带动下移动的距离
*/
@Local IndicatorOffset: number = 0
/**
* 第一个宽度
*/
@Local FirstWidth: number = -1
/**
* 其他的宽度
*/
@Local OtherWidth: number = -1
/**
* Swiper控制器
*/
@Local SwiperController: SwiperController = new SwiperController()
/**
* Swiper容器宽度
*/
@Local SwiperWidth: number = 0
build() {
RelativeContainer() {
Stack() {
Rect()
.height(30)
.stroke(Color.Black)
.radius(10)
.width(this.FirstWidth)
.fill("#bff9f2")
.position({
left: this.IndicatorLeftOffset + this.IndicatorOffset,
bottom: 0
})
.animation({ duration: 300, curve: Curve.LinearOutSlowIn })
}
.width("100%")
.alignRules({
center: { anchor: "Tabs", align: VerticalAlign.Center }
})
Row() {
ForEach(this.TabNames, (name: string, index: number) => {
Row() {
Text(name)
.fontSize(16)
.fontWeight(this.SelectedTabIndex == index ? FontWeight.Bold : FontWeight.Normal)
.textAlign(TextAlign.Center)
.animation({ duration: 300 })
Image($r('app.media.send'))
.width(14)
.height(14)
.margin({ left: 2 })
.visibility(this.SelectedTabIndex == index ? Visibility.Visible : Visibility.None)
.animation({ duration: 300 })
}
.justifyContent(FlexAlign.Center)
.layoutWeight(this.SelectedTabIndex == index ? 1.5 : 1)
.animation({ duration: 300 })
.onClick(() => {
this.SelectedTabIndex = index;
this.SwiperController.changeIndex(index, false);
animateTo({ duration: 500, curve: Curve.LinearOutSlowIn }, () => {
this.IndicatorLeftOffset = this.OtherWidth * index;
})
})
})
}
.width("100%")
.height(30)
.id("Tabs")
.onAreaChange((oldValue: Area, newValue: Area) => {
let tabWidth = newValue.width.valueOf() as number;
this.FirstWidth = 1.5 * tabWidth / (this.TabNames.length + 0.5);
this.OtherWidth = tabWidth / (this.TabNames.length + 0.5);
})
Swiper(this.SwiperController) {
ForEach(this.TabNames, (name: string, index: number) => {
Column() {
Text(`${name} - ${index}`)
.fontSize(24)
.fontWeight(FontWeight.Bold)
}
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.height("100%")
.width("100%")
})
}
.onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
if (targetIndex > index) {
this.IndicatorLeftOffset += this.OtherWidth;
} else if (targetIndex < index) {
this.IndicatorLeftOffset -= this.OtherWidth;
}
this.IndicatorOffset = 0
this.SelectedTabIndex = targetIndex
})
.onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
this.IndicatorOffset = 0
})
.onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
let move: number = this.GetOffset(extraInfo.currentOffset);
//这里需要限制边缘情况
if ((this.SelectedTabIndex == 0 && extraInfo.currentOffset > 0) ||
(this.SelectedTabIndex == this.TabNames.length - 1 && extraInfo.currentOffset < 0)) {
return;
}
this.IndicatorOffset = extraInfo.currentOffset < 0 ? move : -move;
})
.onAreaChange((oldValue: Area, newValue: Area) => {
let width = newValue.width.valueOf() as number;
this.SwiperWidth = width;
})
.curve(Curve.LinearOutSlowIn)
.loop(false)
.indicator(false)
.width("100%")
.id("MainContext")
.alignRules({
top: { anchor: "Tabs", align: VerticalAlign.Bottom },
bottom: { anchor: "__container__", align: VerticalAlign.Bottom }
})
}
.height('100%')
.width('100%')
.padding(10)
}
/**
* 需要注意的点,当前方法仅计算偏移值,不带方向
* @param swiperOffset
* @returns
*/
GetOffset(swiperOffset: number): number {
let swiperMoveRatio: number = Math.abs(swiperOffset / this.SwiperWidth);
let tabMoveValue: number = swiperMoveRatio >= 1 ? this.OtherWidth : this.OtherWidth * swiperMoveRatio;
return tabMoveValue;
}
}
这里实现了新的Tab选项卡的定义。但是没有进行高度封装,想法是方便读者理解组件的使用逻辑,而不是直接提供给读者进行调用。希望这篇文章可以帮助到你~~