简体   繁体   中英

Two-finger swipe gesture on GridView for UWP touch app

I am trying to implement a two-finger swipe gesture for multi-selection of GridView . But I can't receive required pointer events for counting the number of fingers which are touching the screen. I am testing with Dell P2314T during development.

The order of pointer event fireing: Theoretical: PointerPressed (pressed)=> PointerMoved (swiping)=> PointerReleased (released) Actual: PointerPressed & PointerCaptureLost (when pressed but not released)

PS: The demo is removed.

Here is a snippet of my code, which obviously doesn't work because PointerMoved and PointerReleased don't fire:

class CustomGridView : GridView
{
    List<uint> pointerPoint = new List<uint>();
    protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    {
        (element as GridViewItem).AddHandler(PointerPressedEvent, new PointerEventHandler(OnPointerPressed), true);
        (element as GridViewItem).PointerMoved += OnPointerMoved;
        (element as GridViewItem).PointerReleased += OnPointerReleased;
        base.PrepareContainerForItemOverride(element, item);
    }
    private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
    {
        if (!pointerPoint.Contains(e.Pointer.PointerId))
        {
            Debug.WriteLine("add:" + e.Pointer.PointerId);
            pointerPoint.Add(e.Pointer.PointerId);
        }
    }
    private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
    {
        if (pointerPoint.Count == 2)
        {
            Debug.WriteLine("two finger");
        }
    }
    private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
    {
        if (pointerPoint.Contains(e.Pointer.PointerId))
        {
            Debug.WriteLine("rem:" + e.Pointer.PointerId);
            pointerPoint.Remove(e.Pointer.PointerId);
        }
    }
}

This is my solution: Base on observation, ScrollViewer will capture the remaining pointer events. ScrollViewer should be disabled and ongoing direct manipulation should be canceled when PointerPressed is fired.

XAML:

<GridView ScrollViewer.VerticalScrollMode="{x:Bind GridViewScrollMode}"/>

C#:

private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
    pointerPoints.Add(e.Pointer.PointerId);
    if (PointerIds.Count == 2)
    {
        ((sender as GridView).ContainerFromIndex(0) as GridViewItem).CancelDirectManipulations();
        GridViewScrollMode = ScrollMode.Disabled;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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