SwipeView.ets 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { SZYXLocalStorageHelper, ToolsHelper } from '../../../../Index'
  2. @Component
  3. export struct SwipeView {
  4. private _scroller: Scroller = new Scroller()
  5. @State h: number = 40
  6. marginBottom: number = 0
  7. marginTop: number = 0
  8. brtLeft: number = 0
  9. brtRight: number = 0
  10. brbLeft: number = 0
  11. brbRight: number = 0
  12. @State downX: number = 0
  13. onItemClick?: () => void
  14. onDelete?: () => void
  15. ids: string = ToolsHelper.getUuid()
  16. @LocalStorageLink('XBasicSwipeClick') @Watch('onClickIdChange') clickId: string | undefined = undefined
  17. onClickIdChange() {
  18. if (this.clickId !== this.ids) {
  19. this._scroller.scrollTo({
  20. xOffset: 0, yOffset: 0,
  21. animation: { duration: 100, curve: Curve.Linear }
  22. })
  23. }
  24. }
  25. @Builder
  26. doNothingBuilder() {
  27. }
  28. toNumber(len?: Length): number {
  29. if (typeof len === "number") {
  30. return len;
  31. }
  32. if (typeof len === "string") {
  33. return Number(len);
  34. }
  35. return 70
  36. }
  37. @BuilderParam customBuilderParam: () => void = this.doNothingBuilder
  38. // @BuilderParam btnBuilder: () => void = this.doNothingBuilder
  39. build() {
  40. Scroll(this._scroller) {
  41. Row() {
  42. Column() {
  43. this.customBuilderParam()
  44. }.width('100%').onClick(() => {
  45. // SZYXLocalStorageHelper.storage.setOrCreate('XBasicSwipeClick', this.ids)
  46. this._scroller.scrollTo({
  47. xOffset: 0, yOffset: 0,
  48. animation: { duration: 100, curve: Curve.Linear }
  49. })
  50. this.onItemClick && this.onItemClick()
  51. })
  52. Text('删除')
  53. .backgroundColor('red')
  54. .width(70)
  55. .fontColor('white')
  56. .height(this.h - this.marginBottom - this.marginTop)
  57. .textAlign(TextAlign.Center)
  58. .fontSize(16)
  59. .borderRadius({
  60. topLeft: this.brtLeft,
  61. topRight: this.brtRight,
  62. bottomLeft: this.brbLeft,
  63. bottomRight: this.brbRight
  64. })
  65. .onClick(() => {
  66. // SZYXLocalStorageHelper.storage.setOrCreate('XBasicSwipeClick', this.ids)
  67. this._scroller.scrollTo({
  68. xOffset: 0, yOffset: 0,
  69. animation: { duration: 100, curve: Curve.Linear }
  70. })
  71. this.onDelete && this.onDelete()
  72. })
  73. }.onSizeChange((oldValue: SizeOptions, newValue: SizeOptions) => {
  74. this.h = this.toNumber(newValue.height) ?? 40
  75. }).alignItems(VerticalAlign.Center)
  76. }.width('100%').scrollable(ScrollDirection.Horizontal).scrollBar(BarState.Off)
  77. .onTouch((event: TouchEvent) => { // 触摸事件
  78. // 判断是否有打开删除组件,有则关闭
  79. // 根据触摸类型判断
  80. switch (event.type) {
  81. case TouchType.Down: // 触摸按下
  82. SZYXLocalStorageHelper.storage.setOrCreate('XBasicSwipeClick', this.ids)
  83. // 记录按下的x轴坐标
  84. this.downX = event.touches[0].x
  85. break
  86. case TouchType.Up: // 触摸抬起
  87. // 触摸抬起,根据x轴总偏移量,判断是否打开删除
  88. let xOffset = event.touches[0].x - this.downX
  89. // 防止消费点击事件
  90. if (xOffset == 0) {
  91. return
  92. }
  93. // 滑到x轴的位置
  94. let toxOffset = 0
  95. // 开启删除的对象置为null
  96. // 偏移量超过删除按钮一半且左滑,设置打开
  97. if (Math.abs(xOffset) > 35 && xOffset < 0) {
  98. // 删除布局宽度
  99. toxOffset = 70
  100. }
  101. // 滑动指定位置,设置动画
  102. this._scroller.scrollTo({
  103. xOffset: toxOffset, yOffset: 0,
  104. animation: { duration: 300, curve: Curve.Linear }
  105. })
  106. // 重置按下的x轴坐标
  107. this.downX = 0
  108. break
  109. }
  110. })
  111. }
  112. }