【问题标题】:Ionic 2 - setting callback for "long press" event directiveIonic 2 - 为“长按”事件指令设置回调
【发布时间】:2017-09-15 12:17:36
【问题描述】:

我正在尝试在元素上添加自定义 longPress 事件指令,因为 (press)="callback_function()" 将导致 ion-list 无法再滚动。不管有没有错误,我发现我需要添加一个自定义手势指令来添加对新属性的支持,在这种情况下我称之为 longPress。它工作得很好,除了我不知道如何添加回调函数:-)

我已经学习了一个教程 (http://roblouie.com/article/198/using-gestures-in-the-ionic-2-beta/)

“press-directive”在 src/components/press-directive/press-directive.js 中创建,如下所示:

import { Directive, ElementRef, OnInit, OnDestroy } from '@angular/core';
import { Gesture } from "ionic-angular/gestures/gesture";

/**
 * Generated class for the PressDirective directive.
 *
 * See https://angular.io/docs/ts/latest/api/core/index/DirectiveMetadata-class.html
 * for more info on Angular Directives.
 */

@Directive({
  selector: '[longPress]' // Attribute selector
})


export class PressDirective implements OnInit, OnDestroy {
  el: HTMLElement;
  pressGesture: Gesture;

  constructor(el: ElementRef) {
    this.el = el.nativeElement;
  }

  public theCallback() {

  }

  ngOnInit() {
    this.pressGesture = new Gesture(this.el);
    this.pressGesture.listen();

    // instead of this..
    this.pressGesture.on('press', (event) => {
      console.log('pressed!!');
    });

    // i want the callback to come from the template like this:
    // <ion-col (longPress)="showActionSheet(object)">
  }

  ngOnDestroy() {
    this.pressGesture.destroy();
  }
}

home.module.ts 中,我在导入中添加了指令:

import { PressDirective } from "../../components/press-directive/press-directive";

我已经在声明中添加了它:

declarations: [
  Home,
  PressDirective
],

home.html中,我是这样实现的:

<ion-col (longPress)="showActionSheet(relevantObject)">...

我已经删掉了大部分不重要的东西 :-)

当我长按时,它会返回以下内容:

console.log('pressed!!');

但我不知道如何支持模板元素的实际回调函数。

任何帮助或提示将不胜感激..

【问题讨论】:

    标签: angular ionic-framework ionic2


    【解决方案1】:

    对于仍然遇到此问题的任何人,我遇到了非常相似的事情,Steen's answer 在解决添加回调方面非常有帮助。

    但是,我想补充一点,因为我认为应该区分“press”和“release”(或“pressup”)。

    根据HammerJS docs(ionic 用于Gestures),有一个"press" 事件,还有一个"pressup" 事件,它在新闻发布时被触发。

    您实际上可以为每个事件添加一个 @Outputpresspressup):

    /*
     * The first output will emit when the timeout is reached for press,
     * and the second will emit when the press gesture is released.
     */ 
    
    @Output('long-press') onPress: EventEmitter<any> = new EventEmitter();
    @Output('long-press-up') onPressUp: EventEmitter<any> = new EventEmitter();
    

    然后,在 @ngOnInit 中,使用每个相应的发射器响应每个事件:**

    this.pressGesture.on('press', (event) => {
      this.onPress.emit(event);
    });
    
    this.pressGesture.on('pressup', (event) => {
      this.onPressUp.emit(event);
    });
    

    这样,您可以为每个手势事件(在模板/组件中)支持单独的回调函数:

    <ion-col (long-press)="longPressed(object)" (long-press-up)="longPressReleased(object)">
      ...
    </ion-col>
    

    希望这能增加一些信息/清晰度。

    【讨论】:

    • 嗨,我对选择器语法有疑问。我尝试在同一指令中添加 2 个选择器,但它不起作用,我们如何为长按和长按执行此操作-up 在同一指令中。提前致谢。
    • @sur 我相信你只能有一个选择器,但你可以有 2 个OutputEventEmitters(或者与你想要响应的手势数量一样多)。这将让您绑定到这两个事件。我写了一篇关于这个herehere is a gist 的帖子。你问的是这个吗?
    【解决方案2】:

    好的,所以我在很棒的 ionic slack 聊天网站 (https://ionic-worldwide.slack.com) 上被告知了解决方案 - 我需要使用 Output 和 EventEmitter

    导入部分,它必须如下所示:

    import { Directive, ElementRef, OnInit, OnDestroy, Output, EventEmitter } from '@angular/core';
    import { Gesture } from "ionic-angular/gestures/gesture";
    

    中,我必须添加一个@Output EventEmitter

    export class PressDirective implements OnInit, OnDestroy {
      el: HTMLElement;
      pressGesture: Gesture;
      @Output('long-press') onPressRelease: EventEmitter<any> = new EventEmitter();
    

    ngOnInit 内部的 on('press',...) 必须如下所示:

    this.pressGesture.on('press', (event) => {
      this.onPressRelease.emit('released');
    });
    

    现在模板支持添加(long-press)="showActionSheet(object)":

    <ion-col (long-press)="showActionSheet(object)">
    

    是的,我也将它从 longPress 更改为 long-press.. 对我来说看起来更好..

    【讨论】:

    • 如何处理你的发布事件?
    【解决方案3】:

    通过在应用模块中提供 HAMMER_GESTURE_CONFIG,我能够在 Ionic v4 中解决此问题。点击此链接获取解决方案:Vertical scroll is not working with HammerJS and Angular2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2022-01-06
      • 2018-01-10
      • 2016-09-11
      • 1970-01-01
      相关资源
      最近更新 更多