简体   繁体   English

Libgdx 双指平移而不是单指平移?

[英]Libgdx two-finger pan instead of single finger?

I've overridden the LibGdx Pan gesture method for a different functionality (a selector).我已经为不同的功能(选择器)覆盖了 LibGdx Pan 手势方法。 However, I want the pan functionality to use two fingers(pointers) instead.但是,我希望平移功能改为使用两个手指(指针)。 Is this possible?这可能吗?

It's the same question as this post, however his is specific to iPhone and not LibGdx: How to implement the traditional imageView-inside-a-scrollView pan gesture with two fingers instead of one?这与这篇文章的问题相同,但他是针对 iPhone 而不是 LibGdx: 如何用两根手指而不是一根手指实现传统的 imageView-inside-a-scrollView 平移手势?

the pan() method will only fire with one finger, not two. pan() 方法只会用一根手指触发,而不是两根手指。 I was thinking of keeping track of number of fingers used, by setting a variable in touchDown() using the int pointer variable, however the pan() method will not fire when there are 2 fingers in use.我正在考虑通过使用 int 指针变量在 touchDown() 中设置一个变量来跟踪使用的手指数,但是当使用 2 个手指时 pan() 方法不会触发。

Any suggestions?有什么建议? Cheers干杯

我设法围绕pinch()方法来计算组合的两个手指平移和缩放手势,而不是使用zoom()

Here's some code which achieves this using the pinch method, by looking at how far the first finger has been dragged from its initial position. 下面是一些使用pinch方法实现此目的的代码,通过查看第一根手指从其初始位置被拖动的距离。 The second finger is ignored. 第二根手指被忽略了。

Gdx.input.setInputProcessor(new GestureDetector(new GestureDetector.GestureAdapter() {
    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
        float panAmount = pointer1.x - initialPointer1.x;
        // ...
        return true;
    }
})));

After 3+ years of research I finally made it. 经过3年多的研究,我终于成功了。

@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
    // Calculate distances
    float initialDistance = initialPointer1.dst(initialPointer2);
    float distance = pointer1.dst(pointer2);
    // Calculate pinch coordinates
    float initialPinchX = (initialPointer1.x + initialPointer2.x) / 2;
    float initialPinchY = (initialPointer1.y + initialPointer2.y) / 2;
    float pinchX = (pointer1.x + pointer2.x) / 2;
    float pinchY = (pointer1.y + pointer2.y) / 2;
    // This to avoid first time zooming or panning horrible behavior
    if (lastZoomDistance == 0) {
        lastZoomDistance = initialDistance;
    }
    if (lastPinchX == lastPinchY && lastPinchX == 0) {
        lastPinchX = initialPinchX;
        lastPinchY = initialPinchY;
    }
    // Zoom
    float distanceDifference = distance - lastZoomDistance;
    camera.zoom -= distanceDifference / 300;
    // Pan
    float deltaX = (pinchX - lastPinchX) * camera.zoom;
    float deltaY = (pinchY - lastPinchY) * camera.zoom;
    camera.translate(-deltaX, deltaY);
    // We need to update these for future calculations
    lastZoomDistance = distance;
    lastPinchX = (pointer1.x + pointer2.x) / 2;
    lastPinchY = (pointer1.y + pointer2.y) / 2;
    return false;
}

To add onto Luis' answe, sometimes you may want Pan/ Zoom to act as exclusive actions (only one can happen at a time).再加上 Luis 的回答,有时您可能希望 Pan/Zoom 充当专有操作(一次只能发生一个)。 If that's what you're after, this may be useful如果这就是你所追求的,这可能会有用

val targetVector1 = Vector2(pointer1).sub(initialPointer1)
val targetVector2 = Vector2(pointer2).sub(initialPointer2)

val isZoom = targetVector1.hasOppositeDirection(targetVector2)
val isPan = !isZoom

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

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