0

In a UWP application, I am having a scroll view, in which there are multiple canvas as children. I need to restrict scrolling and panning when interacted with single finger and to enable them with two fingers.

I have tried counting the pointers which are currently in contact with the scroll viewer using Pointer events.

Scenario 1: Using PointerPressed and PointerReleased evnets, in this the pointer pressed is not raised when scrolling fast. Also PointerReleased event is not raised when scrolled and lifted the finger.

Scenario 2: Using PointerCaptureLost event, PointerCaptureLost event is raised, and both the pointers are lost when two fingers are in contact with the scroll viewer.

I need to keep track of how many fingers are in contact at a time.

1
  • If you are targeting UWP, then the app is NOT Maui, nor WINUI-3. Please remove those tags, or correct the description of the platform being targeted. Commented Mar 24, 2023 at 19:28

1 Answer 1

0

How to restrict scrolling and panning with single finger and enable with two fingers in UWP ScrollViewer?

Currently, there is no built-in API that could directly track of how many fingers are in contact at a time. But you could do what you want about restrict scrolling and panning with single finger and enable with two fingers in UWP ScrollViewer via pointer events.

Please handle the UIElement.PointerExited Event and UIElement.PointerCaptureLost Event. Now the code should do what you want at the beginning. In my testing it will disable scrolling and panning with single finger and to enable them with two or more fingers.

  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;
        }
    }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.