Vapolia.MauiGesture 1.0.4

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Vapolia.MauiGesture --version 1.0.4                
NuGet\Install-Package Vapolia.MauiGesture -Version 1.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Vapolia.MauiGesture" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Vapolia.MauiGesture --version 1.0.4                
#r "nuget: Vapolia.MauiGesture, 1.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Vapolia.MauiGesture as a Cake Addin
#addin nuget:?package=Vapolia.MauiGesture&version=1.0.4

// Install Vapolia.MauiGesture as a Cake Tool
#tool nuget:?package=Vapolia.MauiGesture&version=1.0.4                

NuGet
NuGet
Nuget

Supported Platforms

iOS, Android, Windows, Mac

Maui Gesture Effects

Add "advanced" gestures to Maui. Available on all views. Most gesture commands include the event position.
Combine this feature with UserInteraction.Menu() to display a standart menu at the position of the finger. Useful especially for tablets. See the demo app in this repo on how to do it.

    <Label Text="Click here" IsEnabled="True" ui:Gesture.TapCommand="{Binding OpenLinkCommand}" />

Or in code:

    var label = new Label();
    Gesture.SetTapCommand(label, new Command(() => { /*your code*/ }));

Quick start

Add the above nuget package to your Maui project
then add this line to your maui app builder:

using MauiGestures;
...
builder.UseAdvancedGestures();

The views on which the gesture is applied should have the property IsEnabled="True" and InputTransparent="False" which activates user interaction on them.

Examples

Add Gesture.TapCommand on any supported xaml view:

        <StackLayout ui:Gesture.TapCommand="{Binding OpenLinkCommand}">
            <Label Text="1.Tap this to open an url"  />
        </StackLayout>

Declare the corresponding namespace:

    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             ...
             xmlns:ui="clr-namespace:MauiGestures;assembly=MauiGestures">

And in the viewmodel:

 public Command OpenLinkCommand => new Command(() =>
 {
     //do something
 });

Supported Gestures

  • TapCommand (ICommand or Command<YourClass>) if CommandParameter is set (see below)
  • DoubleTapCommand (ICommand) or Command<YourClass> if CommandParameter is set (see below)
  • PanCommand (ICommand) or Command<YourClass> if CommandParameter is set (see below)
  • LongPressCommand (ICommand) or Command<YourClass> if CommandParameter is set (see below)
  • TapPointCommand (ICommand or Command<PointEventArgs>)
  • DoubleTapPoinCommand (ICommand or Command<PointEventArgs>)
  • PanPointCommand (ICommand or Command<PanEventArgs>)
  • LongPressPointCommand (ICommand or Command<PointEventArgs>)
  • SwipeLeftCommand (ICommand) or Command<YourClass> if CommandParameter is set (see below)
  • SwipeRightCommand (ICommand) or Command<YourClass> if CommandParameter is set (see below)
  • SwipeTopCommand (ICommand) or Command<YourClass> if CommandParameter is set (see below)
  • SwipeBottomCommand (ICommand) or Command<YourClass> if CommandParameter is set (see below)
  • PinchCommand (Command<PinchEventArgs>) where PinchEventArg contains StartingPoints, CurrentPoints, Center, Scale, RotationRadians, RotationDegrees, Status

PointEventArgs contains the absolute tap position relative to the view, the instance of the control triggering the command, and the BindingContext associated with that control. With that feature, the gestures can easily be used on CollectionView's items.

Properties:

  • IsPanImmediate Set to true to receive the PanCommand or PanPointCommand event on touch down, instead of after a minimum move distance. Default to false.

If you define the CommandParameter property, some gestures will callback the command with this parameter's value.
Example:

<ContentPage x:Name="ThePage" ...>
    <CollectionView ...>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                    <Grid
                      ui:Gesture.TapCommand="{Binding BindingContext.MyItemTappedCommand, Source={x:Reference ThePage}}"
                      ui:Gesture.CommandParameter="{Binding .}">
                        <Label Text="{Binding SomeText}" />
                    </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage

Note that the above example can be simplified by using TapPointCommand instead of TapCommand. TapPointCommand already provides the BindingContext in its PointEventArgs parameter to your command.

Examples

Some commands in XAML

<VerticalStackLayout ui:Gesture.TapCommand="{Binding OpenCommand}" IsEnabled="True">
    <Label Text="1.Tap this text to open an url" />
</VerticalStackLayout>

<VerticalStackLayout ui:Gesture.DoubleTapPointCommand="{Binding OpenPointCommand}" IsEnabled="True">
    <Label Text="2.Double tap this text to open an url" />
</VerticalStackLayout>

<BoxView
    ui:Gesture.PanPointCommand="{Binding PanPointCommand}"
    HeightRequest="200" WidthRequest="300"
    InputTransparent="False"
    IsEnabled="True"
     />

In the viewmodel:

public ICommand OpenCommand => new Command(async () =>
{
   //...
});

public ICommand OpenPointCommand => new Command<PointEventArgs>(args =>
{
    var point = args.Point;
    PanX = point.X;
    PanY = point.Y;
    //...
});

public ICommand PanPointCommand => new Command<PanEventArgs>(args =>
{
    var point = args.Point;
    PanX = point.X;
    PanY = point.Y;
    //...
});

Exemple on a Grid containing an horizontal slider (set value on tap)

//Tap anywhere to set value
Gesture.SetTapPointCommand(this, new Command<PointEventArgs>(args =>
{
    var pt = args.Point;
    var delta = (pt.X - Padding.Left) / (Width - Padding.Left - Padding.Right);
    if(delta<0 || delta>1)
        return;
    Value = (int)Math.Round((Maximum - Minimum) * delta);
}));

Limitations

Only commands are supported (PR welcome for events). No .NET events. So you must use the MVVM pattern.

Swipe commands are not supported on Windows because of a curious bug (event not received). If you find it, notify me! PinchCommand is not supported (yet) on Windows. PR welcome.

If your command is not receiving events, make sure that:

  • you used the correct handler. Ie: the LongPressPointCommand should be new Command<PointEventArgs>(args => ...)
  • you set IsEnabled="True" and InputTransparent="False" on the element

Windows requires the fall creator update.

Alt

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-android34.0 is compatible.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-ios17.2 is compatible.  net8.0-maccatalyst was computed.  net8.0-maccatalyst17.2 is compatible.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net8.0-windows10.0.19041 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.7 226 10/4/2024
1.0.6 397 8/24/2024
1.0.6-ci10533245599 94 8/23/2024
1.0.5 632 5/15/2024
1.0.5-ci9097459940 100 5/15/2024
1.0.4 112 5/15/2024
1.0.4-ci8278984061 300 3/14/2024
1.0.4-ci8250011298 89 3/12/2024
1.0.3 262 3/6/2024
1.0.2 311 1/26/2024
1.0.1 109 1/25/2024
1.0.1-ci7653921426 85 1/25/2024
1.0.1-ci7653640065 78 1/25/2024
1.0.0-ci2501125632 2,240 6/15/2022
1.0.0-ci2495271423 147 6/14/2022
1.0.0-ci2495052041 146 6/14/2022

1.0.2: UseAdvancedGestures instead of AddAdvancedGestures
           1.0.1: net6 to net8
           1.0.0: MAUI initial version