Skip to content

Instantly share code, notes, and snippets.

@b12f
Last active August 28, 2016 18:28
Show Gist options
  • Save b12f/cbcdeda645f13f26ee504e4be5a1e26f to your computer and use it in GitHub Desktop.
Save b12f/cbcdeda645f13f26ee504e4be5a1e26f to your computer and use it in GitHub Desktop.
Angular 2 directive that emits a longpress event after a touch event that lasts 400ms
//<ion-item (click)="open(item)" long-press (longpress)="select(item)"></ion-item>
import {Directive, HostListener, Output, EventEmitter} from '@angular/core';
@Directive({
selector: '[long-press]'
})
export class LongPressDirective {
private touchTimeout: any;
@Output() longpress = new EventEmitter();
private rootPage: any;
constructor() {}
@HostListener('touchstart') touchstart():void {
this.touchTimeout = setTimeout(() => {
this.longpress.emit({});
}, 400);
}
@HostListener('touchend') touchend():void {
this.touchEnd();
}
@HostListener('touchcancel') touchcancel():void {
this.touchEnd();
}
private touchEnd():void {
clearTimeout(this.touchTimeout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment