2

I have a project with ionic/ anuglar 6 and hammerJs which rocks but I have decided to update to anuglar9.1.6 and ionic 5.0.0.

The problem is that my gesture doesn't work anymore ! my press and pressup are not recognized.

However I have imported HammerModule..

Please anyone can help me :/ .. I sure there is something to do

Thank you!

Please find below home.ts and home.html .

app.module:

imports: [HammerModule, BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    {
      provide: HAMMER_GESTURE_CONFIG,
      useClass: IonicGestureConfig
  },
  ],

ionic-gesture.ts

@Injectable()
export class IonicGestureConfig extends HammerGestureConfig {
    buildHammer(element: HTMLElement) {
        const mc = new (<any>window).Hammer(element);

        for (const eventName in this.overrides) {
            if (eventName) {
              mc.get(eventName).set(this.overrides[eventName]);
            }
          }

        return mc;
    }
}

import {Component} from '@angular/core';

@Component({
    selector: 'app-home',
    templateUrl: 'home.page.html',
    styleUrls: ['home.page.scss'],
})
export class HomePage {

    public progress: number = 0;
    public pressState: string = "released";

    // Interval function
    protected interval: any;


    onPress($event) {
        console.log("onPress", $event);
        this.pressState = 'pressing';
        this.startInterval();
    }

    onPressUp($event) {
        console.log("onPressUp", $event);
        this.pressState = 'released';
        this.stopInterval();
    }

    startInterval() {
        const self = this;
        this.interval = setInterval(function () {
            self.progress = self.progress + 1;
        }, 50);
    }

    stopInterval() {
        clearInterval(this.interval);
    }


}
<ion-header>
  <ion-toolbar>
      <ion-title>
          Long Press Demo
      </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content padding>
  <h2> Button Progress: {{ progress }}</h2>
  <h2> Presses State: {{ pressState }}</h2>
  <ion-button (press)="onPress($event)" (pressup)="onPressUp($event)">Press Me</ion-button>
</ion-content>

2
  • just curious: why did you decide to use Hammer and not Ionic built in gestures ?
    – ihor.eth
    Commented Jul 7, 2020 at 15:26
  • I want to implement x and y swipe but ionic gesture is cool for custom gesture. I'm wrong ? By the wait, if you have an idea for x and y swipe with ionic-gesture. I already have x swipe Commented Jul 7, 2020 at 15:43

1 Answer 1

1

I think it might be related to the fact that hammerjs script itself is no longer bundled with Angular since v6.x.

See here: https://angular.io/api/platform-browser/HammerModule

Note that applications still need to include the HammerJS script itself. This module simply sets up the coordination layer between HammerJS and Angular's EventManager.

Try to import hammerjs inside your main.ts and see if this helps to resolve the issue?

npm install hammerjs --save

then inside polyfill.ts or main.ts:

import "hammerjs";
10
  • oh;) bummer. BTW do you have window['Hammer'] in the context of your app? does it initialize properly? this way you could eliminate whats wrong Commented Jul 7, 2020 at 21:04
  • I don't understand, so I don't think so: Can you explain me ? Commented Jul 8, 2020 at 5:16
  • so after your app loaded hit up console and check window object to feature Hammer. Just type window.Hammer Commented Jul 8, 2020 at 5:30
  • I'm sorry, but I don't see where I have to type window.Hammer... Commented Jul 8, 2020 at 6:19
  • use google chrome in dev mode - the console after you do my serve Commented Jul 8, 2020 at 7:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.