[英]Hammer.js set distance to 0 when pan direction is triggered
我正在嘗試修改 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 });
我用一些奇怪的代碼解決了我的問題,如果您只使用平移功能或所有閾值都相等,請使用它。
將此行添加到文件的開頭:
...
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));
},
最后在同一個 PanRecognizer 中添加 if in emit: function(input)
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.