角度为7的ipad双击和长按事件

问题描述 投票:1回答:1

我们如何以适当的方式为ipad实现角度7中的双击和长按事件。

我使用指令自定义实现上述功能。但问题是它正在扼杀滚动功能。

我已经搜索了hammerJS各自的活动,但无法找到答案。

长期指令

 import { Directive, Input, Output, EventEmitter, HostListener, HostBinding } from '@angular/core';
    import { DataService } from '../services/data.service';

    @Directive({
     selector: '[appLongPress]'
     })
    export class LongPressDirective {

    @Input() duration: number = 1500;

    @Output() onLongPress: EventEmitter<any>  = new EventEmitter<any>();
    @Output() onLongPressing: EventEmitter<any>  = new EventEmitter<any>();
    @Output() onLongPressEnd: EventEmitter<any>  = new EventEmitter<any>();

    private pressing: boolean;
    private longPressing: boolean;
    private timeout: any;
    private mouseX:number = 0;
    private mouseY: number= 0;

    constructor(private dataService: DataService) { }

    @HostBinding('class.press')
  get press() { return this.pressing; }

  @HostBinding('class.longpress')
  get longPress() { return this.longPressing; }

  @HostListener('touchstart', ['$event'])
  onTouchStart(event) {
    this.pressing = true;
    this.longPressing = false;
    this.timeout = setTimeout(() => {
      this.longPressing = true;
      // Passing coordinates of the long tapped position
      this.mouseX = event.clientX;
      this.mouseY = event.clientY;
      this.onLongPress.emit(event);
      this.dataService.publishCoordinates({x: this.mouseX, y: this.mouseY});
      this.loop(event);
    }, this.duration);

    this.loop(event);
  }

  @HostListener('mousedown', ['$event'])
  onMouseDown(event) {
    // don't do right/middle clicks
    if(event.which !== 1) {
      return;
    }

    this.mouseX = event.clientX;
    this.mouseY = event.clientY;

    this.pressing = true;
    this.longPressing = false;

    this.timeout = setTimeout(() => {
      this.longPressing = true;
      this.mouseX = event.clientX;
      this.mouseY = event.clientY;
      this.onLongPress.emit(event);
      this.dataService.publishCoordinates({x: this.mouseX, y: this.mouseY});
      this.loop(event);
    }, this.duration);

    this.loop(event);
  }

  loop(event) {
    if(this.longPressing) {
      this.timeout = setTimeout(() => {
        this.onLongPressing.emit(event);
      }, 50);
    }
  }

  endPress() {
    clearTimeout(this.timeout);
    this.longPressing = false;
    this.pressing = false;
    this.onLongPressEnd.emit(true);
  }

  @HostListener('touchend')
  onTouchEnd() { this.endPress(); }

  @HostListener('mouseup')
  onMouseUp() { this.endPress(); }

   }

双击指令

    import { Directive, HostListener, Output, EventEmitter } from '@angular/core';

@Directive({
  selector: '[appDoubleTap]'
})
export class DoubleTapDirective {

  @Output() doubleTap = new EventEmitter();
  @Output() tripleTap = new EventEmitter();

  constructor() { }

  @HostListener('tap',  ['$event'])
  onTap(e) {
    if (e.tapCount === 2) {
      this.doubleTap.emit(e)
    }

    if (e.tapCount === 3) {
      this.tripleTap.emit(e)
    }
  }
}

我使用的模板样本是

 <div appDoubleTap appLongPress (onLongPressing)="lineRowLongPressed(line.lineNum)" (doubleTap)="doubleClick_calender(line)">
                            <div class="long-press-front">
                                <td>
                                    <p>{{line.orderNum}}</p>
                                </td>
                                <td>
                                    <p *ngIf="line.xxx"><img id="xxximg" style="width: 14px;" src="assets/icons/xxx.png"></p>
                                    <p *ngIf="line.yyy"><img id="yyy" style="width: 14px;" src="assets/icons/yyy.png"></p>
                                </td>
                                <td>
                                    <p>x</p>
                                </td>
                            </div>
                            <div class="long-press-dynamic-content" id="sortableLine">
                                <td *ngFor="let col of line.values" class="xxx-col-values">{{col}}</td>
                            </div>
                        </div>

我也尝试过hammerjs(按)事件,但滚动仍然不顺利。还可以设置锤子中按压事件的时间跨度吗?

typescript ipad angular7 hammer.js touch-event
1个回答
0
投票

您可以使用

  1. 定时器实现长时间的挖掘
  2. 来自Jquery手机的Jquery'tamphold'活动
  3. 或者获取一个开源的javascript库或jquery插件来完成这项工作。例如:qazxsw poi(带有polyfill)

看看这个问题,你会发现一个适合你的需求的方式https://pressurejs.com

© www.soinside.com 2019 - 2024. All rights reserved.