gestures.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * 获取触摸点位置信息
  3. */
  4. export const getTouchPoints = (nativeEvent, index = 0) => {
  5. const touches = nativeEvent.touches
  6. const changedTouches = nativeEvent.changedTouches
  7. const hasTouches = touches && touches.length > 0
  8. const hasChangedTouches = changedTouches && changedTouches.length > 0
  9. const points = !hasTouches && hasChangedTouches ? changedTouches[index] : hasTouches ? touches[index] : nativeEvent
  10. return {
  11. x: points.pageX,
  12. y: points.pageY,
  13. }
  14. }
  15. /**
  16. * 获取触摸点个数
  17. */
  18. export const getPointsNumber = (e) => e.touches && e.touches.length || e.changedTouches && e.changedTouches.length
  19. /**
  20. * 判断是否为同一点
  21. */
  22. export const isEqualPoints = (p1, p2) => p1.x === p2.x && p1.y === p2.y
  23. /**
  24. * 判断是否为相近的两点
  25. */
  26. export const isNearbyPoints = (p1, p2, DOUBLE_TAP_RADIUS = 25) => {
  27. const xMove = Math.abs(p1.x - p2.x)
  28. const yMove = Math.abs(p1.y - p2.y)
  29. return xMove < DOUBLE_TAP_RADIUS & yMove < DOUBLE_TAP_RADIUS
  30. }
  31. /**
  32. * 获取两点之间的距离
  33. */
  34. export const getPointsDistance = (p1, p2) => {
  35. const xMove = Math.abs(p1.x - p2.x)
  36. const yMove = Math.abs(p1.y - p2.y)
  37. return Math.sqrt(xMove * xMove + yMove * yMove)
  38. }
  39. /**
  40. * 获取触摸移动方向
  41. */
  42. export const getSwipeDirection = (x1, x2, y1, y2) => {
  43. return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
  44. }