touch_event.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. -- touch_event.lua
  2. local touch_event = {}
  3. -- 事件类型定义
  4. touch_event.EVENT_CLICK = 1 --单击事件
  5. -- touch_event.EVENT_DOUBLE_CLICK = 2 --双击事件,实际用途不大
  6. touch_event.EVENT_LONG_PRESS = 3 --长按事件
  7. touch_event.EVENT_SWIPE_LEFT = 4 --左滑事件
  8. touch_event.EVENT_SWIPE_RIGHT = 5 --右滑事件
  9. touch_event.EVENT_SWIPE_UP = 6 --上滑事件
  10. touch_event.EVENT_SWIPE_DOWN = 7 --下滑事件
  11. -- 内部状态
  12. local state = "IDLE"--默认闲置状态,按压和
  13. local start_time = 0 -- 按下时间戳
  14. local start_x, start_y = 0, 0 --按下初始坐标
  15. local last_x, last_y = 0, 0 --抬手时坐标
  16. local last_move_time = 0 -- 最后移动时间戳
  17. -- 阈值定义(单位:像素)
  18. local CLICK_MAX_MOVE = 50 --长按允许区域,超过算滑动
  19. local SWIPE_THRESHOLD = 70 -- 滑动距离大于70判断为滑动,可以根据屏幕尺寸设置
  20. local SWIPE_RATIO = 2.0 -- 滑动长的距离是滑动短的距离2倍以上,判定长边为滑动方向,斜向滑动且距离相差不在2倍以上,滑动不生效
  21. local LONG_PRESS_DURATION = 500 -- 超过500(毫秒)为长按
  22. -- 初始化手势识别
  23. function touch_event.init()
  24. state = "IDLE"
  25. start_time = 0
  26. start_x, start_y = 0, 0
  27. last_x, last_y = 0, 0
  28. last_move_time = 0
  29. end
  30. -- 计算两点之间的距离
  31. local function distance(x1, y1, x2, y2)
  32. return math.sqrt((x2 - x1)^2 + (y2 - y1)^2)
  33. end
  34. -- 处理触摸事件(只考虑坐标)
  35. function touch_event.process(point)
  36. local x, y, event = point.x, point.y, point.event
  37. local result = nil
  38. local now = point.time or mcu.ticks() -- 获取当前时间戳
  39. if event == 1 then -- 按下
  40. if state == "IDLE" then --闲置状态转为按压状态
  41. state = "PRESSED"
  42. start_time = now
  43. start_x, start_y = x, y
  44. last_x, last_y = x, y
  45. last_move_time = now
  46. end
  47. elseif event == 2 then -- 抬起
  48. if state == "PRESSED" then
  49. local duration = now - start_time -- 按下持续时间
  50. -- 计算移动距离
  51. local dist = distance(start_x, start_y, x, y)
  52. -- 长按检测:时间超过阈值且移动距离在允许范围内
  53. if duration >= LONG_PRESS_DURATION and dist <= CLICK_MAX_MOVE then
  54. result = {tag = touch_event.EVENT_LONG_PRESS, x = start_x, y = start_y}
  55. else
  56. -- 滑动和点击检测逻辑
  57. local dx = x - start_x
  58. local dy = y - start_y
  59. local adx = math.abs(dx)--返回绝对值
  60. local ady = math.abs(dy)
  61. -- 检测滑动主方向移动距离
  62. if adx > ady then--滑动方向为X
  63. if adx > SWIPE_THRESHOLD and adx > ady * SWIPE_RATIO then --符合X轴的滑动判定
  64. if dx < 0 then
  65. result = {tag = touch_event.EVENT_SWIPE_LEFT, x = start_x, y = start_y} --距离减小为左
  66. else
  67. result = {tag = touch_event.EVENT_SWIPE_RIGHT, x = start_x, y = start_y}--记录增大为右
  68. end
  69. end
  70. else
  71. if ady > SWIPE_THRESHOLD and ady > adx * SWIPE_RATIO then--符合Y轴的滑动判定
  72. if dy < 0 then
  73. result = {tag = touch_event.EVENT_SWIPE_UP, x = start_x, y = start_y}
  74. else
  75. result = {tag = touch_event.EVENT_SWIPE_DOWN, x = start_x, y = start_y}
  76. end
  77. end
  78. end
  79. -- 检测点击
  80. if not result and adx <= CLICK_MAX_MOVE and ady <= CLICK_MAX_MOVE then --符合点击判定
  81. result = {tag = touch_event.EVENT_CLICK, x = start_x, y = start_y}
  82. end
  83. end
  84. touch_event.init()
  85. end
  86. elseif event == 3 then -- 移动
  87. if state == "PRESSED" then
  88. last_x, last_y = x, y
  89. last_move_time = now
  90. -- 原有滑动检测逻辑
  91. local dx = x - start_x
  92. local dy = y - start_y
  93. local adx = math.abs(dx)
  94. local ady = math.abs(dy)
  95. -- 检测滑动主方向移动距离
  96. if adx > ady then
  97. if adx > SWIPE_THRESHOLD and adx > ady * SWIPE_RATIO then
  98. if dx < 0 then
  99. result = {tag = touch_event.EVENT_SWIPE_LEFT, x = start_x, y = start_y}
  100. else
  101. result = {tag = touch_event.EVENT_SWIPE_RIGHT, x = start_x, y = start_y}
  102. end
  103. end
  104. else
  105. if ady > SWIPE_THRESHOLD and ady > adx * SWIPE_RATIO then
  106. if dy < 0 then
  107. result = {tag = touch_event.EVENT_SWIPE_UP, x = start_x, y = start_y}
  108. else
  109. result = {tag = touch_event.EVENT_SWIPE_DOWN, x = start_x, y = start_y}
  110. end
  111. end
  112. end
  113. -- 如果检测到滑动,立即返回结果
  114. if result then
  115. touch_event.init()
  116. end
  117. end
  118. end
  119. return result
  120. end
  121. return touch_event