HarmonyOS(47) onSizeChange和onAreaChange

onSizeChange和onAreaChange

onSizeChange

组件区域变化时触发该回调。仅会响应由布局变化所导致的组件尺寸 发生变化时的回调。由绘制变化所导致的渲染属性变化不会响应回调,如translate、offset。

java 复制代码
// xxx.ets
@Entry
@Component
struct AreaExample {
  @State value: string = 'Text'
  @State sizeValue: string = ''

  build() {
    Column() {
      Text(this.value)
        .backgroundColor(Color.Green)
        .margin(30)
        .fontSize(20)
        .onClick(() => {
          this.value = this.value + 'Text'
        })
        .onSizeChange((oldValue: SizeOptions, newValue: SizeOptions) => {
          console.info(`Ace: on size change, oldValue is ${JSON.stringify(oldValue)} value is ${JSON.stringify(newValue)}`)
          this.sizeValue = JSON.stringify(newValue)
        })
      Text('new area is: \n' + this.sizeValue).margin({ right: 30, left: 30 })
    }
    .width('100%').height('100%').margin({ top: 30 })
  }
}

onAreaChange

组件区域 变化时触发该回调。仅会响应由布局变化所导致的组件大小、位置发生变化时的回调。由绘制变化所导致的渲染属性变化不会响应回调,如translate、offset。

dart 复制代码
// xxx.ets
@Entry
@Component
struct AreaExample {
  @State value: string = 'Text'
  @State sizeValue: string = ''

  build() {
    Column() {
      Text(this.value)
        .backgroundColor(Color.Green)
        .margin(30)
        .fontSize(20)
        .onClick(() => {
          this.value = this.value + 'Text'
        })
        .onAreaChange((oldValue: Area, newValue: Area) => {
          console.info(`Ace: on area change, oldValue is ${JSON.stringify(oldValue)} value is ${JSON.stringify(newValue)}`)
          this.sizeValue = JSON.stringify(newValue)
        })
      Text('new area is: \n' + this.sizeValue).margin({ right: 30, left: 30 })
    }
    .width('100%').height('100%').margin({ top: 30 })
  }
}

onAreaChange和onSizeChange的区别

因为size的变化,必定会因此areai变化,所以

  • onSizeChange调用的时候一定会触发onAreaChange
  • onAreaChage调用的时候不一定会触发onSizeChange
    结合PanGesture手势事件,实验代码如下:
dart 复制代码
// xxx.ets
@Entry
@Component
struct PanGestureExample {
  @State offsetX: number = 0
  @State offsetY: number = 0
  positionX: number = 0
  positionY: number = 0
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.All })
  @State mHeight:number = 200;
  build() {
    Column() {
      Column() {
        Text('PanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
      }
      .height(this.mHeight)
      .width(300)
      .padding(20)
      .border({ width: 3 })
      .margin(50)
      .onClick(()=>{
        console.info('Pan click')
        // this.mHeight +=5;
      })  .onAreaChange((oldValue: Area, newValue: Area) => {
        console.error(`PanGesture onAreaChange: on area change, oldValue is ${JSON.stringify(oldValue)} value is ${JSON.stringify(newValue)}`)
      })  .onSizeChange((oldValue: SizeOptions, newValue: SizeOptions) => {
        console.info(`PanGesture onSizeChange: on size change, oldValue is ${JSON.stringify(oldValue)} value is ${JSON.stringify(newValue)}`)
      })
      .translate({ x: this.offsetX, y: this.offsetY }) // 以组件左上角为坐标原点进行移动,此处也可以调用position({ x: this.offsetX, y: this.offsetY }) 方法
      // 左右拖动触发该手势事件
      .gesture(
        PanGesture(this.panOption)
          .onActionStart((event: GestureEvent) => {
            console.info('Pan start')
          })
          .onActionUpdate((event: GestureEvent) => {
            if (event) {
              this.offsetX = this.positionX + event.offsetX
              this.offsetY = this.positionY + event.offsetY
            }
          })
          .onActionEnd((event: GestureEvent) => {
            //记录拖动结束前停留的位置
            this.positionX = this.offsetX
            this.positionY = this.offsetY
            console.info('Pan end')
          })
      )
    }
  }
}

参考资料

HarmonyOS(45) 控件拖动或者拖拽PanGesture
组件尺寸变化事件
组件区域变化事件

相关推荐
Swift社区16 分钟前
HarmonyOS 页面路由与导航开发
华为·harmonyos
希望上岸的大菠萝2 小时前
HarmonyOS 6.0 开发环境搭建完全指南 - DevEco Studio 配置 + 真机调试实战
华为·harmonyos
大雷神2 小时前
HarmonyOS智慧农业管理应用开发教程--高高种地--第28篇:用户中心与个人资料
华为·harmonyos
雨季6662 小时前
破界与共生:HarmonyOS原生应用生态全景图谱与PC时代三重变局
flutter·华为·harmonyos
一路阳光8513 小时前
华为mate80现在确实没有日日新了,看来华为是对鸿蒙6有信心了
华为·harmonyos
三掌柜6663 小时前
如何从一个开发者成为鸿蒙KOL
华为·harmonyos
哈基米~南北绿豆3 小时前
虚拟机体验:在Windows/Mac上运行鸿蒙PC开发环境
windows·macos·harmonyos
爱笑的眼睛113 小时前
学着学着 我就给这个 HarmonyOS 应用增加了些新技术
华为·ai·harmonyos
花花_13 小时前
HarmonyOS开发:字符串全栈实战手册
harmonyos·鸿蒙领航者计划
REDcker3 小时前
鸿蒙系统发展史与纯血鸿蒙详解
华为·harmonyos·鸿蒙·鸿蒙系统