如何实现通过手势涂抹选择文字的效果?
问题效果预览:

@Entry @Component struct TextSelectionDemo { private textContents: number[] = new Array(64).fill(12); @State selectedStatus: boolean[] = new Array(this.textContents.length).fill(false); // textContents选中状态,1选中,0未选中 private textRects: RectText[] = []; // 存储文本区域坐标 endIndexOfLast: number = -1; // 记录上次滑动最后更新的文本索引 upOrDown: number = 0; // 1手指下滑,2手指上滑,记录手指滑动状态,判断是否反向 // 获取当前手势所在文本的索引 touchItemIndex(touchX: number, touchY: number): number { // 检测触摸点是否在文本区域内 let ret: number = -1; for (let index = 0; index < this.textContents.length; ++index) { const rect = this.textRects[index]; if (rect && touchX >= rect.left && touchX <= rect.right && touchY >= rect.top && touchY <= rect.bottom) { ret = index; } } return ret; } updateSelectedIndexes(start: number, end: number) { // 过滤特殊情况:滑动非文本区域||在一个文本区域内滑动(文本会闪烁) if (end === -1 || start === end) { return; } console.info(`endIndex: ${start}, curIndex: ${end}`); // 手指刚按下滑动的情况 if (start === -1) { this.selectedStatus[end] = !this.selectedStatus[end]; this.upOrDown = 0; // 向下滑动 } else if (start < end) { // 特殊情况:手指向上滑动转下滑 if (this.upOrDown === 2) { this.selectedStatus[start] = !this.selectedStatus[start]; } for (let index = start + 1; index <= end; index++) { this.selectedStatus[index] = !this.selectedStatus[index]; } this.upOrDown = 1; // 手指滑动方向设置向下 // 向上滑动 } else if (start > end) { // 特殊情况:手指向下滑动转上滑 if (this.upOrDown === 1) { this.selectedStatus[start] = !this.selectedStatus[start]; } for (let index = start - 1; index >= end; index--) { this.selectedStatus[index] = !this.selectedStatus[index]; } this.upOrDown = 2; // 手指滑动方向设置向上 } this.endIndexOfLast = end; // 记录最后一个更新的文本 } build() { Column() { Scroll() { // 可滑动选择的Flex容器 Flex({ direction: FlexDirection.Row, wrap: FlexWrap.Wrap }) { ForEach(this.textContents, (item: number, index: number) => { Text(`${index}${item}item`) .fontSize(16) .padding(10) .borderRadius(8) .backgroundColor(this.selectedStatus[index] ? '#E6F3FF' : '#F1F3F5') .margin(5) .onAreaChange((oldVal: Area, newVal: Area) => { console.info(`oldVal:${oldVal} newVal:${newVal}`); // 记录每个Text的布局区域 this.textRects[index] = { left: newVal.position.x as number, right: (newVal.position.x as number) + (newVal.width as number), top: newVal.position.y as number, bottom: (newVal.position.y as number) + (newVal.height as number) }; }) .onClick(() => { this.selectedStatus[index] = !this.selectedStatus[index]; }); }); } .gesture( GestureGroup( GestureMode.Sequence, LongPressGesture({ repeat: false }), // 长按手势 PanGesture({ fingers: 1 }) // 长按后滑动 .onActionStart((event: GestureEvent) => { console.info(`${event.offsetX}, ${event.offsetY}`); }) .onActionUpdate((event: GestureEvent) => { let curIndex: number = this.touchItemIndex(event.fingerList[0].localX, event.fingerList[0].localY); console.info(`endIndex: ${this.endIndexOfLast}, curIndex: ${curIndex}`); this.updateSelectedIndexes(this.endIndexOfLast, curIndex); }) .onActionEnd(() => { this.endIndexOfLast = -1; this.upOrDown = 0; }) ) ).width('100%').padding(10); }.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM]) .width('100%').height('100%'); }; } } interface RectText { left: number; right: number; top: number; bottom: number; }
要提取出选中的文本数据进行后续处理,只需遍历一遍文本选中状态数组selectedStatus即可。