xamarin 如何在UWP ScrollViewer中限制单指滚动和平移并启用双指?

lrpiutwd  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(117)

在一个UWP应用程序中,我有一个滚动视图,其中有多个画布作为子视图。我需要限制单指交互时的滚动和平移,并使用两个手指启用它们。
我已经尝试使用指针事件来计算当前与滚动查看器接触的指针。
场景1:使用PointerPressed和PointerReleased事件,在这种情况下,当快速滚动时,按下的指针不会升高。当滚动并抬起手指时,也不会引发PointerReleased事件。
场景2:使用PointerCaptureLost事件,引发PointerCaptureLost事件,当两个手指与滚动查看器接触时,两个指针都丢失。
我需要记录一下一次有多少根手指在接触。

6uxekuva

6uxekuva1#

如何在UWP ScrollViewer中限制单指滚动和平移并启用双指?
目前,没有内置的API可以直接跟踪一次有多少手指接触。但是你可以通过指针事件对restrict scrolling and panning with single finger and enable with two fingers in UWP ScrollViewer做你想做的事情。
请处理UIElement.PointerExited事件和UIElement.PointerCaptureLost事件。现在代码应该在开始时做你想做的事情。在我的测试中,它将禁用单指滚动和平移,并启用两个或多个手指。

public MainPage()
    {
        this.InitializeComponent();
        this.scrollView.AddHandler(UIElement.PointerPressedEvent, new PointerEventHandler(scrollView_PointerPressed), true);
        this.scrollView.AddHandler(UIElement.PointerReleasedEvent, new PointerEventHandler(scrollView_PointerReleased), true);
        this.scrollView.AddHandler(UIElement.PointerCanceledEvent, new PointerEventHandler(scrollView_PointerCanceled), true);
        this.scrollView.AddHandler(UIElement.PointerCaptureLostEvent, new PointerEventHandler(scrollView_PointerCaptureLost), true);
        this.scrollView.AddHandler(UIElement.PointerExitedEvent, new PointerEventHandler(scrollView_PointerExited), true);
        
    }

    int numberOfContacts = 0;
    private void scrollView_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
        {
            numberOfContacts++;
            System.Diagnostics.Debug.WriteLine($"Pressed -> {numberOfContacts}");
            if (numberOfContacts == 1)
            {
                (scrollView.Content as UIElement).ManipulationMode &= ~ManipulationModes.System;
            }
            else if (numberOfContacts > 1)
            {
                (scrollView.Content as UIElement).ManipulationMode |= ManipulationModes.System;
            }
        }
    }

    private void scrollView_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
        {
            if (numberOfContacts > 0)
            {
                numberOfContacts--;
            }
            System.Diagnostics.Debug.WriteLine($"Released -> {numberOfContacts}");
            (scrollView.Content as UIElement).ManipulationMode |= ManipulationModes.System;
        }
    }

    private void scrollView_PointerCanceled(object sender, PointerRoutedEventArgs e)
    {
        if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
        {
            if (numberOfContacts > 0)
            {
                numberOfContacts--;
            }
            System.Diagnostics.Debug.WriteLine($"Cancelled -> {numberOfContacts}");
            (scrollView.Content as UIElement).ManipulationMode |= ManipulationModes.System;
        }
    }

    private void scrollView_PointerCaptureLost(object sender, PointerRoutedEventArgs e)
    {
        if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
        {
            if (numberOfContacts > 0)
            {
                numberOfContacts--;
            }
            System.Diagnostics.Debug.WriteLine($"Capture lost -> {numberOfContacts}");
            (scrollView.Content as UIElement).ManipulationMode |= ManipulationModes.System;
        }
    }

    private void scrollView_PointerExited(object sender, PointerRoutedEventArgs e)
    {
        if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
        {
            if (numberOfContacts>0) 
            {
                numberOfContacts--;
            }
            System.Diagnostics.Debug.WriteLine($"Pointer Exited -> {numberOfContacts}");
            (scrollView.Content as UIElement).ManipulationMode |= ManipulationModes.System;
        }
    }

相关问题