通过添加用户自定义布局Span绑定手势实现缩放,具体思路如下:
@Component struct PinchGestureExample { @State scaleValue: number = 1; @State pinchValue: number = 1; @State pinchX: number = 0; @State pinchY: number = 0; build() { Stack({ alignContent: Alignment.TopStart }) { Image($r('app.media.startIcon')) .height('100%') .width('100%') .objectFit(ImageFit.Contain) .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 }) // 2指捏合触发该手势事件 .gesture( PinchGesture({ fingers: 2 }) .onActionStart(() => { console.info('Pinch start'); }) .onActionUpdate((event: GestureEvent) => { if (event) { this.scaleValue = this.pinchValue * event.scale; this.pinchX = event.pinchCenterX; this.pinchY = event.pinchCenterY; } }) .onActionEnd(() => { this.pinchValue = this.scaleValue; console.info('Pinch end'); }) ); }.width('100%'); } } @Entry @Component struct PinchGestureIndex { controller: RichEditorController = new RichEditorController(); option: RichEditorOptions = { controller: this.controller }; private myOffset: number | undefined = undefined; private myBuilder: CustomBuilder = undefined; @Builder placeholderBuilder() { Row({ space: 2 }) { PinchGestureExample(); }.width(200).height(200).padding(5); } build() { Column() { Column() { RichEditor(this.option) .onReady(() => { }) .borderWidth(1) .borderColor('#0A59F7') .width('100%') .height('30%'); Button('add span') .onClick(() => { this.myBuilder = () => { this.placeholderBuilder(); }; let num = this.controller.addBuilderSpan(this.myBuilder, { offset: this.myOffset }); console.info(`addBuilderSpan return ${num}`); }) .margin(16); } .borderWidth(1) .borderColor('#0A59F7') .width('100%') .height('70%'); } .padding(16); } }