From 9c004926b1125c11b597c702179b01b042ea7a9b Mon Sep 17 00:00:00 2001 From: liugang9704 <2745340733@qq.com> Date: Thu, 10 Jul 2025 20:22:26 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9EFAQ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/pages/CustomRateLimiting.ets | 72 +++++++++++++++++ .../main/ets/pages/GestureRateLimiting.ets | 46 +++++++++++ .../src/main/ets/pages/InertialRolling.ets | 80 +++++++++++++++++++ 3 files changed, 198 insertions(+) create mode 100644 ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets create mode 100644 ArkUI/entry/src/main/ets/pages/GestureRateLimiting.ets create mode 100644 ArkUI/entry/src/main/ets/pages/InertialRolling.ets diff --git a/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets b/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets new file mode 100644 index 0000000..3e95de6 --- /dev/null +++ b/ArkUI/entry/src/main/ets/pages/CustomRateLimiting.ets @@ -0,0 +1,72 @@ +/* +* 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: 如何对手势事件进行限流?例如500ms内不允许点击事件重复触发? +*/ + +// [Start gesture_modifier] +class MyGesture implements GestureModifier { + // 防重点击间隔时间 + interval: number = 500; + // 上次点击时间 + lastTime: number = 0; + func: Function = () => { + } + + applyGesture(event: UIGestureEvent): void { + event.addGesture( + new TapGestureHandler({ count: 1, fingers: 1 }) + .onAction((event: GestureEvent) => { + const nowTime = Date.now(); + const remainTime = this.interval - (nowTime - this.lastTime); + if (remainTime <= 0) { + this.lastTime = nowTime; + this.func(); + } + }) + ) + } +} + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + @State modifier: MyGesture = new MyGesture(); + + onPageShow(): void { + this.modifier.func = () => { + console.info('---onTap---'); + } + } + + build() { + RelativeContainer() { + Button(this.message) + .id('click') + .fontSize(50) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: "__container__", align: VerticalAlign.Center }, + middle: { anchor: "__container__", align: HorizontalAlign.Center } + }) + .gestureModifier(this.modifier) + } + .height('100%') + .width('100%') + } +} +// [End gesture_modifier] \ No newline at end of file diff --git a/ArkUI/entry/src/main/ets/pages/GestureRateLimiting.ets b/ArkUI/entry/src/main/ets/pages/GestureRateLimiting.ets new file mode 100644 index 0000000..f5150aa --- /dev/null +++ b/ArkUI/entry/src/main/ets/pages/GestureRateLimiting.ets @@ -0,0 +1,46 @@ +/* +* 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: 如何对手势事件进行限流?例如500ms内不允许点击事件重复触发? +*/ + +// [Start throttle] +// 定义限流函数 +function throttle(func: Function, interval: number) { + let lastTime = 0; + return () => { + const nowTime = Date.now(); + const remainTime = interval - (nowTime - lastTime); + if (remainTime <= 0) { + lastTime = nowTime; + func(); + } + }; +} +// [End throttle] + +// 对Button的点击事件进行节流,500ms内不能重复触发 +@Entry +@Component +struct Index { + build() { + // [Start on_click] + Button('防止重复点击').onClick(throttle(() => { + // do something + }, 5000)) + // [End on_click] + } +} \ No newline at end of file diff --git a/ArkUI/entry/src/main/ets/pages/InertialRolling.ets b/ArkUI/entry/src/main/ets/pages/InertialRolling.ets new file mode 100644 index 0000000..22f463e --- /dev/null +++ b/ArkUI/entry/src/main/ets/pages/InertialRolling.ets @@ -0,0 +1,80 @@ +/* +* 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: 如何通过PanGesture手势或者SwipeGesture手势实现自定义组件的惯性滚动效果 +*/ + +// [Start pan_gesture_example] +@Entry +@Component +struct PanGestureExample { + @State offsetX: number = 0; + @State offsetY: number = 0; + @State positionX: number = 0; + @State positionY: number = 0; + private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Up | PanDirection.Down }); + + build() { + Column() { + Text('PanGesture offset: \nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY) + } + .height(200) + .width(200) + .padding(20) + .border({ width: 3 }) + .margin(30) + // 以组件左上角为坐标原点进行移动 + .translate({ + x: this.offsetX, + y: this.offsetY, + z: 0 + }) + .gesture( + // 拖动 + PanGesture(this.panOption) + .onActionStart((event?: GestureEvent) => { + console.info('Pan start'); + }) + .onActionUpdate((event?: GestureEvent) => { + if (event) { + // 最后的位置加上偏移量 + this.offsetX = this.positionX + event.offsetX; + this.offsetY = this.positionY + event.offsetY; + } + }) + .onActionEnd((event) => { + this.offsetX = this.positionX + event.offsetX; + this.offsetY = this.positionX + event.offsetY; + this.positionX = this.positionX + event.offsetX; + this.positionY = this.positionY + event.offsetY; + let ySpeed = event.velocityY; + this.getUIContext().animateTo({ + duration: 1000, + curve: Curve.LinearOutSlowIn, + iterations: 1, + playMode: PlayMode.Normal, + onFinish: () => { + console.info('play end'); + } + }, () => { + this.offsetY = this.offsetY + ySpeed * 0.2; + this.positionY = this.positionY + ySpeed * 0.2; + }) + }) + ) + } +} +// [End pan_gesture_example] \ No newline at end of file -- Gitee From ff3cf28ca79247f3e7b1fb4f05ec331cf8bb1a0a Mon Sep 17 00:00:00 2001 From: liugang9704 <2745340733@qq.com> Date: Tue, 15 Jul 2025 19:58:54 +0800 Subject: [PATCH 2/4] =?UTF-8?q?FAQ:Swiper=E5=A6=82=E4=BD=95=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=AF=BC=E8=88=AA=E7=82=B9=E9=AB=98=E5=BA=A6?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E6=96=B0=E5=A2=9E=E6=96=B9=E6=A1=88=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pages/CustomNavigationPointsPlanTwo.ets | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 ArkUI/entry/src/main/ets/pages/CustomNavigationPointsPlanTwo.ets diff --git a/ArkUI/entry/src/main/ets/pages/CustomNavigationPointsPlanTwo.ets b/ArkUI/entry/src/main/ets/pages/CustomNavigationPointsPlanTwo.ets new file mode 100644 index 0000000..5de454b --- /dev/null +++ b/ArkUI/entry/src/main/ets/pages/CustomNavigationPointsPlanTwo.ets @@ -0,0 +1,60 @@ +/* +* Copyright (c) 2025 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:Swiper如何自定义导航点高度位置 +*/ + +@Entry +@Component +struct DotIndicatorDemo { + private indicatorController: IndicatorComponentController = new IndicatorComponentController(); + private swiperController: SwiperController = new SwiperController(); + @State list: string[] = ['1', '2', '3', '4', '5', '6']; + + build() { + Stack({ alignContent: Alignment.Bottom }) { + Swiper(this.swiperController) { + ForEach(this.list, (item: string) => { + Text(item) + .width('90%') + .height(200) + .backgroundColor(0xAFEEEE) + .textAlign(TextAlign.Center) + .fontSize(30) + }, (item: string) => item) + } + .cachedCount(2) + .index(0) + .indicator(this.indicatorController) + + Column() { + IndicatorComponent(this.indicatorController) + .style( + new DotIndicator() + .itemWidth(10) + .itemHeight(10) + .selectedItemWidth(20) + .selectedItemHeight(10) + .color(Color.White) + .selectedColor(Color.Gray) + ) + } + .height(25) + .clip(true) + } + .width('100%') + } +} \ No newline at end of file -- Gitee From 2c460884bdc7fc5a5f891426a2b4fce1c776a8f6 Mon Sep 17 00:00:00 2001 From: liugang9704 <2745340733@qq.com> Date: Tue, 15 Jul 2025 20:00:18 +0800 Subject: [PATCH 3/4] =?UTF-8?q?FAQ:Swiper=E5=A6=82=E4=BD=95=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=AF=BC=E8=88=AA=E7=82=B9=E9=AB=98=E5=BA=A6?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E6=96=B0=E5=A2=9E=E6=96=B9=E6=A1=88=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/pages/CustomNavigationPointsPlanTwo.ets | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ArkUI/entry/src/main/ets/pages/CustomNavigationPointsPlanTwo.ets b/ArkUI/entry/src/main/ets/pages/CustomNavigationPointsPlanTwo.ets index 5de454b..2bb06b8 100644 --- a/ArkUI/entry/src/main/ets/pages/CustomNavigationPointsPlanTwo.ets +++ b/ArkUI/entry/src/main/ets/pages/CustomNavigationPointsPlanTwo.ets @@ -17,6 +17,7 @@ * FAQ:Swiper如何自定义导航点高度位置 */ +// [Start DotIndicatorDemo] @Entry @Component struct DotIndicatorDemo { @@ -57,4 +58,5 @@ struct DotIndicatorDemo { } .width('100%') } -} \ No newline at end of file +} +// [Start DotIndicatorDemo] \ No newline at end of file -- Gitee From 4d38726fbc68dda953def7ce5ff601f3254ee760 Mon Sep 17 00:00:00 2001 From: liugang9704 <2745340733@qq.com> Date: Tue, 15 Jul 2025 20:11:03 +0800 Subject: [PATCH 4/4] =?UTF-8?q?FAQ:=E5=A6=82=E4=BD=95=E9=80=9A=E8=BF=87Pan?= =?UTF-8?q?Gesture=E6=89=8B=E5=8A=BF=E6=88=96=E8=80=85SwipeGesture?= =?UTF-8?q?=E6=89=8B=E5=8A=BF=E5=AE=9E=E7=8E=B0=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E7=9A=84=E6=83=AF=E6=80=A7=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArkUI/entry/src/main/ets/pages/InertialRolling.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ArkUI/entry/src/main/ets/pages/InertialRolling.ets b/ArkUI/entry/src/main/ets/pages/InertialRolling.ets index 22f463e..036ca2b 100644 --- a/ArkUI/entry/src/main/ets/pages/InertialRolling.ets +++ b/ArkUI/entry/src/main/ets/pages/InertialRolling.ets @@ -57,7 +57,7 @@ struct PanGestureExample { }) .onActionEnd((event) => { this.offsetX = this.positionX + event.offsetX; - this.offsetY = this.positionX + event.offsetY; + this.offsetY = this.positionY + event.offsetY; this.positionX = this.positionX + event.offsetX; this.positionY = this.positionY + event.offsetY; let ySpeed = event.velocityY; -- Gitee