
温馨提示:本篇博客的详细代码已发布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下载运行哦!

本文将详细解析TabsRaisedCircle组件的核心实现,包括状态管理、布局结构和交互处理。

@Component
export struct TabsRaisedCircle {
@Link @Watch("getAnimateSelectIndex") selectIndex: number;
@State animateSelectIndex: number = 0;
@Prop tabHeight: number = 60;
@Link tabsMenu: TabMenusInterfaceIRequired[];
@Prop tabsBgColor: Color = Color.White;
@Prop tabsSelectBgColor: string = "rgba(92, 187, 183,1)";
@Prop tabsFontColor: Color = Color.Black;
@Prop tabsSelectFontColor: Color = Color.Black;
@State chamfer: ChamferInfo | undefined = undefined;
@State selectImageInfo: RaisedSelectImageInfo | undefined = undefined;
}selectIndex: 当前选中项animateSelectIndex: 动画控制索引chamfer: 凸起圆信息selectImageInfo: 选中图片信息tabHeight: 导航栏高度tabsBgColor: 背景颜色tabsSelectBgColor: 选中状态颜色tabsFontColor: 文字颜色build() {
RelativeContainer() {
ForEach(this.tabsMenu, (item: TabMenusInterfaceIRequired, index: number) => {
this.TabItem(item, index)
})
if (this.chamfer) {
TabsRaisedCircleSelect({
tabHeight: this.tabHeight,
selectIndex: this.selectIndex,
chamfer: this.chamfer,
selectBodyId: this.selectBodyId,
tabItemId: this.tabItemId,
tabsBgColor: this.tabsBgColor,
tabsSelectBgColor: this.tabsSelectBgColor,
})
}
}
.width("100%")
.height(this.tabHeight)
.backgroundColor(this.tabsBgColor)
.id(this.tabsId)
}RelativeContainer@Builder
TabItem(item: TabMenusInterfaceIRequired, index: number) {
Column() {
if (item.image && this.chamfer) {
Image(getImageUrl(item as TabMenusInterfaceIRequired, index, this.selectIndex))
.size({
width: this.chamfer.circleDiameter / 2,
height: this.chamfer.circleDiameter / 2
})
.interpolation(ImageInterpolation.High)
.offset({
y: this.selectIndex === index && this.animateSelectIndex === index ?
-(this.getCountOffsetY()) : 0,
})
.id(`${this.selectImageId}${index}`)
}
Text(item.text)
.fontColor(this.selectIndex === index ?
(item.tabsSelectFontColor || this.tabsSelectFontColor) :
(item.tabsFontColor || this.tabsFontColor))
}
.onClick(() => {
animateTo({
duration: this.animateTime,
}, () => {
this.selectIndex = index
})
})
.width(100 / this.tabsMenu.length + "%")
.height("100%")
.justifyContent(FlexAlign.Center)
.id(`${this.tabItemId}${index}`)
}getCountOffsetY() {
if (this.selectImageInfo && this.chamfer) {
return this.selectImageInfo.getCenterOffsetY() -
(this.chamfer.circleRadius - this.chamfer.circleOffsetY)
}
return 0
}TabsRaisedCircle组件通过:
实现了一个:
的导航栏组件。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。