SwipeView.ets 3.4 KB

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