简体   繁体   English

无需抬起手指即可检测滑动手势

[英]Detect swipe gesture without lifting the finger

I am developing a game for android with libgdx and I have a little problem, I want to make it trigger a function when the user swipes, this I achieve. 我正在用libgdx为Android开发游戏,我有一点问题,我想让它在用户滑动时触发一个功能,这是我实现的。 The problem is that it runs the function when you finish swiping (when you lift your finger). 问题是当你完成滑动(抬起手指)时它会运行该功能。 How can I make the function run while the swipe is doing? 如何在滑动操作时使功能运行?

This is the current code for the gesture listener: 这是手势监听器的当前代码:

private static class DirectionGestureListener extends GestureAdapter{

    DirectionListener directionListener;

    public DirectionGestureListener(DirectionListener directionListener){
        this.directionListener = directionListener;
    }

    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        if(Math.abs(velocityX)>Math.abs(velocityY)){
            if(velocityX>0){
                directionListener.onRight();
            }else{
                directionListener.onLeft();
            }
        }else{
            if(velocityY>0){
                directionListener.onDown();
            }else{
                directionListener.onUp();
            }
        }
        return super.fling(velocityX, velocityY, button);
    }

}

And the game scene: 和游戏场景:

Gdx.input.setInputProcessor(new SimpleDirectionGestureDetector(new SimpleDirectionGestureDetector.DirectionListener() {
    @Override
    public void onUp() { 
        /*something*/ 
    }

    @Override
    public void onRight() { 
        /*something*/ 
    }

    @Override
    public void onLeft() { 
        /*something*/ 
    }

    @Override
    public void onDown() { 
        /*something*/
    }
}));

I looked into GestureDetector.java of libgdx source code itself. 我看着GestureDetector.java的libgdx源代码本身。 fling() will be executed whenever event of touchUp() happened. fling()时的事件将被执行touchUp()发生。 Thus aligned with what you experienced. 因此与您所经历的一致。

I think an option to make this work is to implement such gesture detection yourself by extending InputAdapter class or implementing InputProcessor interface class, then work on touchDown() , and touchDragged() to have an intended effect as you aimed for. 我认为实现这项工作的一个选择是通过扩展InputAdapter类或实现InputProcessor接口类来自己实现这种手势检测,然后使用touchDown()touchDragged()来达到预期的效果。

The idea is to keep track of touch id as it firstly touched on the screen inside touchDown() (its 3rd parameter is int button which is what you're looking at), then use that id to check and further operate inside touchDragged() . 我的想法是跟踪触摸id,因为它首先触摸屏幕内的touchDown() (它的第三个参数是int button ,这是你正在看的),然后使用该ID检查并进一步在touchDragged()内部操作。 If id matches, then for simple approach, you can check whether user touched and moved for pre-defined distance by comparing it against original touching position. 如果id匹配,那么对于简单的方法,您可以通过将用户与原始触摸位置进行比较来检查用户是否触摸并移动了预定距离。

Let's say we want a swipe gesture only when user firstly touches the screen, moves it for (at least) distance we pre-defined set, and within pre-defined duration. 假设我们只想在用户首次触摸屏幕时移动它,将其移动到(至少)我们预定义设置的距离,并且在预定义的持续时间内。 So if user firstly touches the screen, moves finger around but still not more than pre-defined distance, then finally moves far enough within duration we've set, this still won't be treated as swipe as our conditions set that it must be from first intention (first touch) to do such gesture. 因此,如果用户首先触摸屏幕,移动手指但仍然不超过预定义的距离,然后最终在我们设置的持续时间内移动足够远,这仍然不会被视为滑动,因为我们的条件设置必须是从第一个意图(第一次触摸)做这样的手势。 This means we calculate distance against original touching point, not moved point. 这意味着我们计算距原始触摸点的距离,而不是移动点。 Of course, you can customize the conditions to suit your need too. 当然,您也可以根据自己的需要定制条件。

From above, conditions to regard it as swipe gesture can include following (you can adapt these yourself) 从上面看,将其视为轻扫手势的条件可以包括以下(您可以自己调整这些)

  • distance it moved compared to original touching point 与原始触摸点相比,它移动的距离
  • duration it took to move (might be 150ms, etc), longer duration user can be more relaxing not to act fast to make it registered as swipe gesture 它需要移动的持续时间 (可能是150ms等),持续时间更长的用户可以更放松,不要快速行动,使其注册为轻扫手势
  • only 1 swipe at a time can be taken into effect ie if user uses 2 fingers (2 touches) to swipe at the same time, only first one (or last one) can be used as swipe effect, etc. 一次只能进行1次滑动即可实现,即如果用户同时使用2个手指(2个触摸)进行滑动,则只能使用第一个(或最后一个)作为滑动效果等。

Treating it separately for x, y direction is per your need. 根据您的需要单独处理x,y方向。 If so, you have to add handling code inside touchDragged() to check for both direction and send you corresponding event ie swipe-x, or swipe-y per se. 如果是这样,您必须在touchDragged()添加处理代码以检查两个方向并向您发送相应的事件,即swipe-x或swipe-y本身。 Then you hook it up by calling one of your method to let your game knows. 然后你通过调用你的一个方法来解决它,让你的游戏知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM