From b57c12179ab7e746b774349a82efc695993c047e Mon Sep 17 00:00:00 2001 From: liugang9704 <2745340733@qq.com> Date: Tue, 16 Sep 2025 11:33:58 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E4=BF=AE=E6=94=B9=E5=90=8C=E6=BA=90?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/pages/CustomRateLimiting.ets | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets b/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets index 2936fde..58ad29c 100644 --- a/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets +++ b/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets @@ -19,32 +19,47 @@ // [Start gesture_modifier] class MyGesture implements GestureModifier { - interval: number = 500; - func: ((event: GestureEvent) => void) | null = null; + interval: number = 2000; + private inThrottle: boolean = false; + private lastGestureType: string = ''; - throttle(func: (event: GestureEvent) => void, delay?: number) { - let inThrottle: boolean = false; - return (event: GestureEvent) => { - if (!inThrottle) { - func(event); - inThrottle = true; - setTimeout(() => { - inThrottle = false; - }, delay ? delay : 1000); - } - }; + // Unified rate limiting processing + private throttleWrapper(eventType: string, callback: () => void) { + if (!this.inThrottle) { + this.inThrottle = true; + this.lastGestureType = eventType; + callback(); + + setTimeout(() => { + this.inThrottle = false; + this.lastGestureType = ''; + }, this.interval); + } } applyGesture(event: UIGestureEvent): void { - const throttledFunc = this.throttle((gestureEvent: GestureEvent) => { - if (this.func) { - this.func(gestureEvent); - } - }, this.interval); + // Create a unified gesture processing function + const handleTap = (gestureEvent: GestureEvent) => { + this.throttleWrapper('tap', () => { + console.info('---onTap---'); + }); + }; + const handleLongPress = (gestureEvent: GestureEvent) => { + this.throttleWrapper('longPress', () => { + console.info('---onLongPress---'); + }); + }; + + // Add two gesture recognizers event.addGesture( new TapGestureHandler({ count: 1, fingers: 1 }) - .onAction(throttledFunc) + .onAction(handleTap) + ); + + event.addGesture( + new LongPressGestureHandler({ fingers: 1, duration: 600 }) + .onAction(handleLongPress) ); } } @@ -55,13 +70,6 @@ struct Index { @State message: string = 'Hello World'; @State modifier: MyGesture = new MyGesture(); - onPageShow(): void { - // Note: func now accepts a (event: GestureEvent) => void - this.modifier.func = (event: GestureEvent) => { - console.info('---onTap---'); - }; - } - build() { RelativeContainer() { Button(this.message) @@ -78,4 +86,5 @@ struct Index { .width('100%') } } + // [End gesture_modifier] \ No newline at end of file -- Gitee