From 5cb2bcbe5d3ae687ed5bc0f5d8f8f10fc6e8644b Mon Sep 17 00:00:00 2001 From: ywcoder <1104410818@qq.com> Date: Thu, 18 Sep 2025 17:16:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EFAQ:=E5=A6=82=E4=BD=95?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=BD=93=E9=95=BF=E6=8C=89=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E5=90=8E=EF=BC=8C=E7=A7=BB=E5=87=BA=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=8F=96=E6=B6=88=E5=BD=93=E5=89=8D=E9=95=BF=E6=8C=89?= =?UTF-8?q?=E6=89=8B=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...elLongPressEventWhenRemovingComponents.ets | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 ArkUI/entry/src/main/ets/pages/CancelLongPressEventWhenRemovingComponents.ets diff --git a/ArkUI/entry/src/main/ets/pages/CancelLongPressEventWhenRemovingComponents.ets b/ArkUI/entry/src/main/ets/pages/CancelLongPressEventWhenRemovingComponents.ets new file mode 100644 index 0000000..8f253fa --- /dev/null +++ b/ArkUI/entry/src/main/ets/pages/CancelLongPressEventWhenRemovingComponents.ets @@ -0,0 +1,110 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:如何实现当长按触发成功后,移出组件取消当前长按手势 +*/ + +// [Start CancelLongPressEventWhenRemovingComponents] +@Entry +@Component +struct CancelLongPressEventWhenRemovingComponents { + private recWidth: number = 200; + private recHeight: number = 100; + @State isLongPressing: boolean = false; + @State shouldCancel: boolean = false; + @State touchInBounds: boolean = true; + + build() { + Column() { + // Touchable area. + Stack() { + Text(this.isLongPressing ? 'Long Pressing...' : 'Long press me') + .fontSize(20) + .fontColor(Color.White) + } + .width(this.recWidth) + .height(this.recHeight) + .backgroundColor(this.isLongPressing ? Color.Red : Color.Blue) + .gesture( + LongPressGesture({ repeat: false }) + .onAction(() => { + if (!this.shouldCancel && this.touchInBounds) { + this.handleLongPressStart(); + } + }) + .onActionEnd(() => { + this.handleLongPressEnd(); + }) + ) + .onTouch((event: TouchEvent) => { + this.handleTouchEvent(event); + }) + .onClick(() => { + console.info('onClick'); + }) + .width(this.recWidth) + .height(this.recHeight) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + .alignItems(HorizontalAlign.Center) + } + + // Handling touch events. + private handleTouchEvent(event: TouchEvent) { + const touchX = event.touches[0].x; + const touchY = event.touches[0].y; + // Calculate whether the touch point is within the component boundary. + const inBounds = touchX >= 0 && touchX <= this.recWidth && touchY >= 0 && touchY <= this.recHeight; + switch (event.type) { + case TouchType.Down: + this.shouldCancel = false; + this.touchInBounds = true; + break; + case TouchType.Move: + // Update touch position status. + this.touchInBounds = inBounds; + // If moving out of the boundary and long pressing, cancel. + if (!inBounds && this.isLongPressing) { + this.shouldCancel = true; + this.isLongPressing = false; + console.info('LongPressCancel', 'Slide beyond the boundary, cancel long press.'); + } + break; + case TouchType.Up: + this.shouldCancel = true; + this.touchInBounds = true; + break; + } + } + + // Long press to start processing. + private handleLongPressStart() { + console.info('LongPress', 'Long press to start'); + this.isLongPressing = true; + } + + // Long press to end processing. + private handleLongPressEnd() { + console.info('LongPress', 'Long press to end'); + this.isLongPressing = false; + this.shouldCancel = false; + } +} + +// [End CancelLongPressEventWhenRemovingComponents] \ No newline at end of file -- Gitee