3
votes

Is there a way to use pinch to zoom in shared Xamarin Forms, I only found an implementation for each platform.

1

1 Answers

2
votes

You can use the Pan Gesture to implement it. There is a good sample of wrapping an image in PanContainer available here - Adding a Pan Gesture Recognizer.

The pan gesture is used for detecting dragging and is implemented with the PanGestureRecognizer class. A common scenario for the pan gesture is to horizontally and vertically drag an image, so that all of the image content can be viewed when it's being displayed in a viewport smaller than the image dimensions. This is accomplished by moving the image within the viewport.

Simple Example :

<Image Source="MonoMonkey.jpg">
  <Image.GestureRecognizers>
    <PanGestureRecognizer PanUpdated="OnPanUpdated" />
  </Image.GestureRecognizers>
</Image>

Pan Container example XAML:

<AbsoluteLayout>
    <local:PanContainer>
        <Image Source="MonoMonkey.jpg" WidthRequest="1024" HeightRequest="768" />
    </local:PanContainer>
</AbsoluteLayout>

Code behind :

public class PanContainer : ContentView
{
  double x, y;

  public PanContainer ()
  {
    // Set PanGestureRecognizer.TouchPoints to control the
    // number of touch points needed to pan
    var panGesture = new PanGestureRecognizer ();
    panGesture.PanUpdated += OnPanUpdated;
    GestureRecognizers.Add (panGesture);
  }

 void OnPanUpdated (object sender, PanUpdatedEventArgs e)
 {
   switch (e.StatusType) {
   case GestureStatus.Running:
     // Translate and ensure we don't pan beyond the wrapped user interface element bounds.
     Content.TranslationX =
      Math.Max (Math.Min (0, x + e.TotalX), -Math.Abs (Content.Width - App.ScreenWidth));
     Content.TranslationY =
      Math.Max (Math.Min (0, y + e.TotalY), -Math.Abs (Content.Height - App.ScreenHeight));
     break;

   case GestureStatus.Completed:
     // Store the translation applied during the pan
     x = Content.TranslationX;
     y = Content.TranslationY;
     break;
   }
 }
}