【问题标题】:Hammer.js set distance to 0 when pan direction is triggered触发平移方向时,Hammer.js 将距离设置为 0
【发布时间】:2014-10-30 15:16:12
【问题描述】:

我正在尝试修改 Hammer.js,因此当触发平移时,我希望距离等于 0,因此用户需要移动足够的距离才能再次触发平移而不取消平移。

现在当平移距离大于平移阈值时,如果向任意方向移动1px,就会触发平移方向。

我也不想在触发时强制取消平移,因为用户需要再次单击才能启动平移事件。

这是触发平移方向时的触发代码。

emit: function(input) {
    this.pX = input.deltaX;
    this.pY = input.deltaY;

    var direction = directionStr(input.direction);
    if (direction) {
        this.manager.emit(this.options.event + direction, input);
    }

    this._super.emit.call(this, input);
}

我尝试将 input.distance 修改为 0,但它只影响该函数中的变量,而不影响实际值。

然后我尝试添加这个:

    mc.add( new Hammer.Pan({ direction: Hammer.DIRECTION_ALL, threshold: 10 }) );

但这会取消当前的平移事件。

还尝试取消当前平移并将其激活到等于 0 的距离,但它也取消当前平移...

    mc.get('pan').set({ enable: false });
    mc.get('pan').set({ enable: true });

【问题讨论】:

    标签: javascript hammer.js


    【解决方案1】:

    我用一些奇怪的代码解决了我的问题,如果您只使用平移功能或所有阈值都相等,请使用它

    将此行添加到文件的开头:

    ...
    var abs = Math.abs;
    var now = Date.now;
    //Add emitPanDirection
    var emitPanDirection;
    ...
    

    然后将此添加到 computeInputData 函数

    function computeInputData(manager, input) {
    ....
        input.angle = getAngle(offsetCenter, center);
        input.distance = getDistance(offsetCenter, center);
    
        //This will reset firstInput.center and firstMultiple.center to current mouse location
        if(input.distance > manager.options.preset[3][1].threshold){
            emitPanDirection=true;
            session.firstInput.center=getCenter(pointers);
            session.firstMultiple.center=getCenter(pointers);
        }
    ....
    }
    

    在inherit(PanRecognizer, AttrRecognizer, {...

    attrTest: function(input) {
        return AttrRecognizer.prototype.attrTest.call(this, input) &&
            (this.state & STATE_BEGAN || (!(this.state & STATE_BEGAN) && this.directionTest(input)));
    },
    

    对于这个:

    attrTest: function(input) {
        return (AttrRecognizer.prototype.attrTest.call(this, input) && this.directionTest(input));
    },
    

    最后添加这个 if in emit: function(input) in the same PanRecognizer

    emit: function(input) {
        //Need to add this if or emit will emit pandirection twice
        if(emitPanDirection){
            emitPanDirection=false;
            this.pX = input.deltaX;
            this.pY = input.deltaY;
    
            var direction = directionStr(input.direction);
            if (direction) {
                this.manager.emit(this.options.event + direction, input);
            }
    
            this._super.emit.call(this, input);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多