How to obtain the absolute position in .NET MAUI's PanGestureRecognizer?

85 Views Asked by At

I have a ViewGraphics in my XAML defined like this:

 <GraphicsView x:Name="Canvas" Drawable="{StaticResource Renderer}">
     <GraphicsView.GestureRecognizers>
         <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
         <PanGestureRecognizer PanUpdated="PanGestureRecognizer_PanUpdated" />
     </GraphicsView.GestureRecognizers>
 </GraphicsView>

And the corresponding implementation of PanGestureRecognizer_PanUpdated looks like this:

   private void PanGestureRecognizer_PanUpdated(object sender, PanUpdatedEventArgs e)
   {
    Debug.WriteLine($"{e.StatusType} x:= {e.TotalX}|y:= {e.TotalY}");
   }

The event only gives me the total changes since beginning but there is no way to access the point TotalX and TotalY are relative to. How can I obtain the position (just like TappedEventArgs provides a .GetPosition method)?

I already asked chatgpt but it stucked since it didnt understand that TotalX and TotalY only give the changes with respect to the point I want to find.

1

There are 1 best solutions below

0
FreakyAli On

Okay I am guessing you want that x and y when you pan, to get that you can probably calculate it something like this

 var x = Math.Max(0, e.TotalX);
 if (x > (Width - Thumb.Width))
 x = (Width - Thumb.Width);

This would give you the current y, You would generally do this in the Running state(e.StatusType) otherwise this will always be zero.

You can similarly calculate the y as well