thumbmunkeys
thumbmunkeys

Reputation: 20764

Number of Touchpoints in GestureRecognizer

I am using the GestureRecognizer to detect drag and pinch gestures.

The ManipulationStarted, ManipulationUpdated and ManipulationCompleted events provide the translation and scale values that are needed to pinch and drag.

However I cant figure out how to distinguish between drag (1 touch point) and pinch (2 touch points) gestures. There is no information about the number of touchpoints in GestureRecognizer.

How can I distinguish between drag and pinch with the GestureRecognizer?

Upvotes: 2

Views: 790

Answers (2)

Chris Leyva
Chris Leyva

Reputation: 3526

Well, I feel it is very hacky (as most solutions seem to be for a useable WinRT app) but you can create a List<uint> to keep track of the number of pointers that are currently down on the screen. You would have to handle the PointerPressed event on whatever control you are interacting with (Let's say you are using a Canvas) to "capture" the pointers as they are pressed. That is where you would populate the List<uint>. Don't forget to clear the list at the end of the ManipulationCompleted event as well as any event that would fire upon the end of any gestures (like PointerReleased, PointerCanceled, and PointerCaptureLost). Maybe it would be a good idea to make sure the list is cleared in the ManipulationStarted event. Perhaps you can try that and see how that works for you.

In the ManipulationCompleted event, you can check if your List contains exactly 2 elements (PointerIds). If so, then you know it is a pinch/zoom.

Here is what it could look like:

private void Canvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    var ps = e.GetIntermediatePoints(null);
    if (ps != null && ps.Count > 0)
    {
        this.gestureRecognizer.ProcessDownEvent(ps[0]);
        this.pointerList.Add(e.Pointer.PointerId);
        e.Handled = true;
    }
}

private void gestureRecognizer_ManipulationCompleted(GestureRecognizer sender, ManipulationCompletedEventArgs args)
{         
    if (this.pointerList.Count == 2)
    {
       // This could be your pinch or zoom.
    }
    else
    {
       // This could be your drag.
    }

    // Don't forget to clear the list.
    this.pointerList.Clear();
}

// Make sure you clear your list in whatever events make sense.
private void Canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    this.pointerList.Clear();
}

private void Canvas_PointerCanceled(object sender, PointerRoutedEventArgs e)
{
    this.pointerList.Clear();
}

Upvotes: 2

I have been struggling with the same question for a few hours now and it looks WinRT platform does not provide that. What it instead provides is Delta.Rotation and Delta.Scale values in addition to Delta.Translation with the arguments to ManipulationUpdated callback.

If Delta.Rotation is 0 (or very close to zero - because it is a float value) and Delta.Scale is 1 (or very close to 1), you can conclude that a pinch operation is not the case and a drag operation is being carried otherwise it is a pinch operation. It is not the best you can get but it looks it is the only availability for the time being.

Upvotes: 2

Related Questions