Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App won't recognize swipes using Hammer.JS and HammerGestureConfig in Angular 9

I can't recognize swipes in my Angular app using Hammer.JS. It's setup like this:

"@angular/core": "~9.0.0-next.6",
"hammerjs": "^2.0.8",
"zone.js": "~0.10.2"

app.module.ts is looking like this:

import { BrowserModule, HammerGestureConfig, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';
import * as hammer from 'hammerjs';

export class MyHammerConfig extends HammerGestureConfig {
  overrides = <any>{
    swipe: { direction: hammer.DIRECTION_HORIZONTAL },
    pinch: { enable: false },
    rotate: { enable: false }
  };
}

@NgModule({
  imports: [
    BrowserModule,
  ],
  providers: [
    {
      provide: HAMMER_GESTURE_CONFIG,
      useClass: MyHammerConfig
    }
  ],
})

app.component.ts has this method:

onSwipe() {
  console.log('swipe');
}

And finaly app.component.html looks like this:

<div (swipeleft)="onSwipe()" (swiperight)="onSwipe()">
  <h1>Swipe here</h1>
</div>

However, neither swipeleft or swiperight are triggered ever using an iPad or iPhone both running iOS 13.

Am I missing any crucial configuration? Or do I have another issue with this code?


I also tested this Stackblitz "blog-ng-swiping" which is working fine on the touch devices, but it's using Angular 8.

like image 513
lampshade Avatar asked Feb 21 '20 09:02

lampshade


People also ask

How do you use Hammerjs in angular 9?

The most important thing is to import the HammerModule in app module file. In your angular project, install the hammerjs package locally by running the below command. Now, you will need to import the hammerjs module in your main. ts file.

Do I need Hammer JS?

Since HammerJS is no longer needed when updating to v9, the dependency on HammerJS can be removed if it's not used directly in your application. In some cases, projects use HammerJS events in templates while relying on Angular Material modules to set up the HammerJS event plugin.

What is Hammer JS used for?

“Hammer is an open-source library that can recognize gestures made by touch, mouse, and pointer events.” – hammerjs.github.io. It is a popular JavaScript library that can be used to build web applications that require performing actions like panning, swiping, rotating, and zooming on touch gestures.

How to install hammerjs in Angular 2?

The most important thing is to import the HammerModule in app module file. In your angular project, install the hammerjs package locally by running the below command. Now, you will need to import the hammerjs module in your main.ts file.

How to set up gestures in hammerjs?

The approach is to install the hammerjs package locally, import it in main.ts and set the Hammer gesture configuration by extending the HammerGestureConfig class. Then you can bind to specific events like swipe, pan, pinch, press, etc. The most important thing is to import the HammerModule in app module file.

What are the gestures supported by angular?

A set of supported event names for gestures to be used in Angular. Angular supports all built-in recognizers, as listed in HammerJS documentation. Maps gesture event names to a set of configuration options that specify overrides to the default values for specific properties.

How to bind events to specific events in hammerjs?

Then you can bind to specific events like swipe, pan, pinch, press, etc. The most important thing is to import the HammerModule in app module file. In your angular project, install the hammerjs package locally by running the below command.


1 Answers

It turns out that Angular 9 has a new HammerModule which is used for everything Hammer.JS related and also for tree shaking during compilation.

So besides the whole configuratonion of Hammer.JS, gestures etc. you need to include this Module in your app as well to make Hammer.JS work with Angular 9:

import { HammerModule } from '@angular/platform-browser';

@NgModule({
  imports: [
    HammerModule
  ]
)}
like image 154
lampshade Avatar answered Oct 20 '22 12:10

lampshade