Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class SwipeHandlerService {
- private xDown: number = null;
- private yDown: number = null;
- public init(): void {
- document.addEventListener('touchstart', this.handleTouchStart, false);
- document.addEventListener('touchmove', this.handleTouchMove, false);
- }
- public destroy(): void {
- document.removeEventListener('touchstart', this.handleTouchStart, false);
- document.removeEventListener('touchmove', this.handleTouchMove, false);
- }
- private handleTouchStart(event: any): void {
- const firstTouch = event.touches[0];
- this.xDown = firstTouch.clientX;
- this.yDown = firstTouch.clientY;
- }
- private handleTouchMove(event: any): void {
- if (!this.xDown || !this.yDown) {
- return;
- }
- const xUp = event.touches[0].clientX;
- const yUp = event.touches[0].clientY;
- const xDiff = this.xDown - xUp;
- const yDiff = this.yDown - yUp;
- if (Math.abs(xDiff) > Math.abs(yDiff)) {
- if (xDiff > 0) {
- // swipe left
- } else {
- // swipe right
- }
- } else {
- if (yDiff > 0) {
- // swipe up
- } else {
- // swipe down
- }
- }
- /* reset values */
- this.xDown = null;
- this.yDown = null;
- }
- }
Add Comment
Please, Sign In to add comment