HarmonyOS onAreaChange
onAreaChange 参数两个一直相同变化,而且就算我滑动没有切换参数也会变影响取值,这个方法有替换的吗?
// xxx.ets
import { ComponentUtils } from '@kit.ArkUI'
@Entry
@Component
struct TabsExample {
@State currentIndex: number = 0
@State animationDuration: number = 300
@State indicatorLeftMargin: number = 0
@State indicatorWidth: number = 0
private tabsWidth: number = 0
private componentUtils: ComponentUtils = this.getUIContext().getComponentUtils()
@Builder
tabBuilder(index: number, name: string) {
Column() {
Text(name)
.fontSize(16)
.fontColor(this.currentIndex === index ? '#007DFF' : '#182431')
.fontWeight(this.currentIndex === index ? 500 : 400)
.id(index.toString())
.onAreaChange((oldValue: Area,newValue: Area) => {
if (this.currentIndex === index && (this.indicatorLeftMargin === 0 || this.indicatorWidth === 0)){
if (newValue.position.x != undefined) {
let positionX = Number.parseFloat(newValue.position.x.toString())
this.indicatorLeftMargin = Number.isNaN(positionX) ? 0 : positionX
}
let width = Number.parseFloat(newValue.width.toString())
this.indicatorWidth = Number.isNaN(width) ? 0 : width
}
console.log("aaa"+newValue.width)
})
}.width('100%')
}
build() {
Stack({ alignContent: Alignment.TopStart }) {
Tabs({ barPosition: BarPosition.Start }) {
TabContent() {
Column().width('100%').height('100%').backgroundColor('#00CB87')
}.tabBar(this.tabBuilder(0, 'green'))
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'))
}
.onAreaChange((oldValue: Area,newValue: Area)=> {
let width = Number.parseFloat(newValue.width.toString())
this.tabsWidth = Number.isNaN(width) ? 0 : width
})
.barWidth('100%')
.barHeight(56)
.width('100%')
.height(296)
.backgroundColor('#F1F3F5')
.animationDuration(this.animationDuration)
.onChange((index: number) => {
this.currentIndex = index // 监听索引index的变化,实现页签内容的切换。
})
.onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
// 切换动画开始时触发该回调。下划线跟着页面一起滑动,同时宽度渐变。
this.currentIndex = targetIndex
let targetIndexInfo = this.getTextInfo(targetIndex)
this.startAnimateTo(this.animationDuration, targetIndexInfo.left, targetIndexInfo.width)
})
.onAnimationEnd((index: number,event: TabsAnimationEvent) => {
// 切换动画结束时触发该回调。下划线动画停止。
let currentIndicatorInfo = this.getCurrentIndicatorInfo(index,event)
this.startAnimateTo(0,currentIndicatorInfo.left,currentIndicatorInfo.width)
})
.onGestureSwipe((index: number,event: TabsAnimationEvent) => {
// 在页面跟手滑动过程中,逐帧触发该回调。
let currentIndicatorInfo = this.getCurrentIndicatorInfo(index,event)
this.currentIndex = currentIndicatorInfo.index
this.indicatorLeftMargin = currentIndicatorInfo.left
this.indicatorWidth = currentIndicatorInfo.width
})
Column()
.height(2)
.width(this.indicatorWidth)
.margin({ left: this.indicatorLeftMargin, top:48})
.backgroundColor('#007DFF')
}.width('100%')
}
private getTextInfo(index: number): Record<string, number> {
let rectangle = this.componentUtils.getRectangleById(index.toString())
return { 'left': px2vp(rectangle.windowOffset.x), 'width': px2vp(rectangle.size.width) }
}
private getCurrentIndicatorInfo(index: number, event: TabsAnimationEvent): Record<string, number> {
let nextIndex = index
if (index > 0 && event.currentOffset > 0) {
nextIndex--
} else if (index < 3 && event.currentOffset < 0) {
nextIndex++
}
let indexInfo = this.getTextInfo(index)
let nextIndexInfo = this.getTextInfo(nextIndex)
let swipeRatio = Math.abs(event.currentOffset / this.tabsWidth)
let currentIndex = swipeRatio > 0.5 ? nextIndex : index // 页面滑动超过一半,tabBar切换到下一页。
let currentLeft = indexInfo.left + (nextIndexInfo.left - indexInfo.left) * swipeRatio
let currentWidth = indexInfo.width + (nextIndexInfo.width - indexInfo.width) * swipeRatio
return { 'index': currentIndex, 'left': currentLeft, 'width': currentWidth }
}
private startAnimateTo(duration: number, leftMargin: number, width: number) {
animateTo({
duration: duration, // 动画时长
curve: Curve.Linear, // 动画曲线
iterations: 1, // 播放次数
playMode: PlayMode.Normal, // 动画模式
onFinish: () => {
console.info('play end')
}
}, () => {
this.indicatorLeftMargin = leftMargin
this.indicatorWidth = width
})
}
}
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
HarmonyOS onAreaChange使用问题
1151浏览 • 1回复 待解决
HarmonyOS onAreaChange回调方法问题
541浏览 • 1回复 待解决
HarmonyOS 除了onAreachange之外的获取组件中心点的方式
119浏览 • 1回复 待解决
HarmonyOS如何获取component的坐标/宽高,除了onAreaChange获取之外?
582浏览 • 1回复 待解决
HarmonyOS Swiper如何在监控到onAreaChange事件的时候进行其他界面的刷新
395浏览 • 1回复 待解决
HarmonyOS ArkUI-C的onAreaChange回调中无法得到组件可视区域的信息
210浏览 • 1回复 待解决
HarmonyOS onAreaChange此方法是变化时回调,没找到初始化完成的方法
670浏览 • 1回复 待解决
HarmonyOS 组件配置offset属性后,组件的onAreaChange中的值,position不正确
1026浏览 • 1回复 待解决
HarmonyOS swiper在滑动时子组件的onAreaChange会被调用多次?有没有获取组件position的方法
628浏览 • 1回复 待解决
HarmonyOS 想要实现上下两个文案等宽等高,底部文案是动态变动的大小,除了用onAreaChange还有其他办法吗?
421浏览 • 1回复 待解决
现在很多API返回的UI相关变量都是使用Length来作为类型返回的,开发中此处返回都可以被转成实际数字吗?
467浏览 • 1回复 待解决
HarmonyOS Length 如何转换为具体数值?
718浏览 • 1回复 待解决
Canvas制作图表如何实现滑动惯性?
861浏览 • 1回复 待解决
HarmonyOS Tabs组件在滑动过程中监听TabContent位置变化
221浏览 • 1回复 待解决
HarmonyOS Scroll 嵌套 RelativeContainer 问题
845浏览 • 1回复 待解决
HarmonyOS Scroll嵌套RelativeContainer 问题
679浏览 • 1回复 待解决
HarmonyOS 获取List中当前屏幕可见的ListItem
1064浏览 • 1回复 待解决
HarmonyOS jsbridge HarmonyOS版本
382浏览 • 1回复 待解决
HarmonyOS
218浏览 • 1回复 待解决
HarmonyOS 关于HarmonyOS应用的备案
311浏览 • 1回复 待解决
HarmonyOS flutter接入HarmonyOS原生视图
526浏览 • 1回复 待解决
HarmonyOS HarmonyOS社区组件问题
664浏览 • 1回复 待解决
HarmonyOS 关于HarmonyOS应用启动文件配置
213浏览 • 1回复 待解决
HarmonyOS RN如何获取HarmonyOS AppStorage信息
180浏览 • 1回复 待解决
HarmonyOS 现在HarmonyOS都有支持哪些插件
442浏览 • 1回复 待解决
根据这个demo改下吧: