double-tap.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Directive, ElementRef, OnInit, OnDestroy, Output, EventEmitter } from "@angular/core";
  2. import { Gesture } from "ionic-angular/gestures/gesture";
  3. @Directive({
  4. selector: "[doubleTap]"
  5. })
  6. export class DoubleTap implements OnInit, OnDestroy {
  7. el: HTMLElement;
  8. tapGesture: Gesture;
  9. lastTimeStamp: number = 0;
  10. @Output()
  11. doubleTap: EventEmitter<any> = new EventEmitter();
  12. constructor(el: ElementRef) {
  13. this.el = el.nativeElement;
  14. }
  15. /**
  16. *
  17. */
  18. ngOnInit() {
  19. this.tapGesture = new Gesture(this.el);
  20. this.tapGesture.listen();
  21. this.tapGesture.on("tap", e => {
  22. let delay = e.timeStamp - this.lastTimeStamp;
  23. if (this.lastTimeStamp == 0 || delay > 500) {
  24. this.lastTimeStamp = e.timeStamp;
  25. return;
  26. }
  27. this.lastTimeStamp = 0;
  28. this.doubleTap.emit(e);
  29. });
  30. }
  31. /**
  32. *
  33. */
  34. ngOnDestroy() {
  35. this.tapGesture.destroy();
  36. }
  37. }