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