回复动作的滑动手势

时间:2016-12-12 16:57:13

标签: javascript leap-motion

我有这段代码:



var controller = new Leap.Controller({enableGestures: true});
controller.on('gesture', function (gesture){
    console.log(gesture);
    if(gesture.type === 'swipe'){
        handleSwipe(gesture);
    }
});
function handleSwipe (swipe){
    var startFrameID;
    if(swipe.state === 'stop'){
        if (swipe.direction[0] > 0){
            //this means that the swipe is to the right direction
			slideTimer = setTimeout(function(){
			slidePict("sx");
			},500);
        }else{
            //this means that the swipe is to the left direction
			slideTimer = setTimeout(function(){
			slidePict("dx");
			},500);
        }
    }
}
controller.connect();




它工作得很好,因为识别滑动手势,但是当手势执行时,无论是向左还是向右,幻灯片似乎都会接收两个连续输入,然后拍摄两个/三个幻灯片...... 我怎么能避免这个?

1 个答案:

答案 0 :(得分:0)

滑动手势是由指尖产生的 - 而不是手 - 因此每帧每手最多可以获得5次滑动事件。然后,在下一帧中,您将获得更新每个手势属性的滑动事件。

您可以按如下方式修改代码,以便在允许其他操作开始之前等待当前的滑动操作完成:

var isSwiping = false;
var controller = new Leap.Controller({enableGestures: true});
controller.on('gesture', function (gesture){
    console.log(gesture);
    if(gesture.type === 'swipe' && !isSwiping){
        isSwiping = true;
        handleSwipe(gesture);
    }
});
function handleSwipe (swipe){
    var startFrameID;
        if (swipe.direction[0] > 0){
            //this means that the swipe is to the right direction
            slideTimer = setTimeout(function(){
            slidePict("sx");
            isSwiping = false;
            },500);
        }else{
            //this means that the swipe is to the left direction
            slideTimer = setTimeout(function(){
            slidePict("dx");
            isSwiping = false;
            },500);
        }
}
controller.connect();

[edit]从if(swipe.state == "stop"){}函数中移除了handleSwipe()子句。