HarmonyOS 组件旋转后,平移的x y轴也跟着变了?

给图片组件添加缩放,旋转,平移手势。组件旋转180°后,往上滑动,组件会往下面平移。

阅读 551
1 个回答

应该是组合手势和rotate冲突,可以在image外面套一层父组件参考下面demo:

build() {
  Column() {
    Text('标题栏')
      .width('100%')
      .height(50)
      .textAlign(TextAlign.Center)
    Column(){
      Image($r('app.media.img'))
        .width(200)
        .height(200)
        .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 })
        .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
        .rotate({
          angle: this.angle,
        })
    }.gesture(GestureGroup(GestureMode.Parallel,
      PanGesture()
        .onActionUpdate((event: GestureEvent) => {
          if (event) {
            this.offsetX = this.positionX + event.offsetX
            this.offsetY = this.positionY + event.offsetY
          }
        })
        .onActionEnd(() => {
          this.positionX = this.offsetX
          this.positionY = this.offsetY
          console.info('Pan end')
        }),
      RotationGesture()
        .onActionUpdate((event: GestureEvent) => {
          if (event) {
            this.angle = this.rotateValue + event.angle
          }
        })
        .onActionEnd((event: GestureEvent) => {
          this.rotateValue = this.angle
          console.info('Rotation end')
        })
    )
    )
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进