Explorar el Código

feat(SwipeView): 优化滑动视图的点击和滚动逻辑

- 添加 UUID 生成和本地存储功能,用于唯一标识每个 SwipeView 实例
- 实现点击 ID 变化时自动滚动到起始位置的功能
- 在触摸事件中添加本地存储操作,确保滑动操作的正确性
徐勤民 hace 3 semanas
padre
commit
fbc929fcf8
Se han modificado 1 ficheros con 16 adiciones y 0 borrados
  1. 16 0
      src/main/ets/view/SwipeView.ets

+ 16 - 0
src/main/ets/view/SwipeView.ets

@@ -1,3 +1,5 @@
+import { SZYXLocalStorageHelper, ToolsHelper } from '../../../../Index'
+
 @Component
 export struct SwipeView {
   private _scroller: Scroller = new Scroller()
@@ -5,6 +7,17 @@ export struct SwipeView {
   @State downX: number = 0
   onItemClick?: () => void
   onDelete?: () => void
+  ids: string = ToolsHelper.getUuid()
+  @LocalStorageLink('XBasicSwipeClick') @Watch('onClickIdChange') clickId: string | undefined = undefined
+
+  onClickIdChange() {
+    if (this.clickId !== this.ids) {
+      this._scroller.scrollTo({
+        xOffset: 0, yOffset: 0,
+        animation: { duration: 100, curve: Curve.Linear }
+      })
+    }
+  }
 
   @Builder
   doNothingBuilder() {
@@ -20,6 +33,7 @@ export struct SwipeView {
         Column() {
           this.customBuilderParam()
         }.width('100%').onClick(() => {
+          // SZYXLocalStorageHelper.storage.setOrCreate('XBasicSwipeClick', this.ids)
           this._scroller.scrollTo({
             xOffset: 0, yOffset: 0,
             animation: { duration: 100, curve: Curve.Linear }
@@ -35,6 +49,7 @@ export struct SwipeView {
           .textAlign(TextAlign.Center)
           .fontSize(16)
           .onClick(() => {
+            // SZYXLocalStorageHelper.storage.setOrCreate('XBasicSwipeClick', this.ids)
             this._scroller.scrollTo({
               xOffset: 0, yOffset: 0,
               animation: { duration: 100, curve: Curve.Linear }
@@ -58,6 +73,7 @@ export struct SwipeView {
       // 根据触摸类型判断
       switch (event.type) {
         case TouchType.Down: // 触摸按下
+          SZYXLocalStorageHelper.storage.setOrCreate('XBasicSwipeClick', this.ids)
           // 记录按下的x轴坐标
           this.downX = event.touches[0].x
           break