Xamarin.Forms: using the PanGestureRecognizer

Recently I’ve blogged about Xamarin.Forms and how to create a XAML Behavior for enabling Multi-Touch gestures to generic elements and implementing a scale / pinch functionality.

Fortunately the framework provides three types of recognizer that greatly simplify the implementation:

  • PinchGestureRecognizer allows user interactions for zoom / scale functionalities;
  • PanGestureRecognizer enables pan / translate transformations;
  • TapGestureRecognizer detects tap events.

Yesterday I decided to try the PanGestureRecognizer for extending the capabilities of the Behavior described in the previous post.

First of all, I added two Bindable properties in order to permit activation / deactivation of individual gestures (Bindable properties are equivalent to Dependency ones in UWP XAML)

In this way we can specify in our XAML what gestures are enabled:

Then I initialised the GestureRecognizers adding a new PanGestureRecognizer to the recognizers list:

And subscribed to the PanUpdated event in order to apply the translate transform:

The implementation of this event handler permits to update the X and Y coordinates of the element when a Pan gesture is detected:

Here we go: the sample app can now be deployed to the emulators and iOS / Android / Windows devices.

Just a couple of notes:

  • deployment to iOS required this workaround to work properly since the new sample app uses different assemblies;
  • Tap gestures on Android requires Xamarin.Forms – minimum version v2.1.0.6501-pre1 as pointed by the official documentation.

As usual, the latest source code is available for download on GitHub.

2 thoughts on “Xamarin.Forms: using the PanGestureRecognizer”

  1. Dear Sir,
    I am a .net developer. I have been working in asp.net mvc, c# and at present working also with xamarin forms. I am quite motivated and impressed to see that you have several certifications you have attained so far.

    I have also dream to be MCSD and Xamarin certified. And to begin with MCSD first, I am planning to collect some valuable tutorials and resources so that I can make my dream true.

    So I humbly request you if you can help me regarding this matter providing some valuable suggestions and ideas.

    I would be very grateful to you.

    Looking forward to hear from you.

Leave a Reply

Your email address will not be published. Required fields are marked *