Guest User

Untitled

a guest
Nov 14th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. export class SwipeHandlerService {
  2. private xDown: number = null;
  3. private yDown: number = null;
  4.  
  5. public init(): void {
  6. document.addEventListener('touchstart', this.handleTouchStart, false);
  7. document.addEventListener('touchmove', this.handleTouchMove, false);
  8. }
  9.  
  10. public destroy(): void {
  11. document.removeEventListener('touchstart', this.handleTouchStart, false);
  12. document.removeEventListener('touchmove', this.handleTouchMove, false);
  13. }
  14.  
  15. private handleTouchStart(event: any): void {
  16. const firstTouch = event.touches[0];
  17. this.xDown = firstTouch.clientX;
  18. this.yDown = firstTouch.clientY;
  19. }
  20.  
  21. private handleTouchMove(event: any): void {
  22. if (!this.xDown || !this.yDown) {
  23. return;
  24. }
  25.  
  26. const xUp = event.touches[0].clientX;
  27. const yUp = event.touches[0].clientY;
  28.  
  29. const xDiff = this.xDown - xUp;
  30. const yDiff = this.yDown - yUp;
  31.  
  32. if (Math.abs(xDiff) > Math.abs(yDiff)) {
  33. if (xDiff > 0) {
  34. // swipe left
  35. } else {
  36. // swipe right
  37. }
  38. } else {
  39. if (yDiff > 0) {
  40. // swipe up
  41. } else {
  42. // swipe down
  43. }
  44. }
  45.  
  46. /* reset values */
  47. this.xDown = null;
  48. this.yDown = null;
  49. }
  50. }
Add Comment
Please, Sign In to add comment