Skip to content

Commit

Permalink
Pinch Gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlacey committed Feb 24, 2025
1 parent e03748f commit 4ae23fd
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions NuGet/RapidXaml.MAUI/GestureRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,45 @@ public static ICommand GetSwipeDown(BindableObject view)
public static void SetSwipeDown(BindableObject view, ICommand value)
=> view.SetValue(SwipeDownProperty, value);


/// <summary>
/// Command to execute when the view is pinched. The command will receive a <see cref="PinchGestureUpdatedEventArgs"/> as a parameter.
/// </summary>
public static readonly BindableProperty PinchProperty =
BindableProperty.CreateAttached("Pinch", typeof(Command<PinchGestureUpdatedEventArgs>), typeof(GestureRecognizer), defaultValue: null, propertyChanged: OnPinchChanged);

private static void OnPinchChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is View view)
{
if (oldValue is ICommand oldCmd)
{
for (int i = view.GestureRecognizers.Count; i >= 0; i--)
{
if (view.GestureRecognizers[i] is PinchGestureRecognizer oldPgr)
{
view.GestureRecognizers.Remove(oldPgr);
break;
}
}
}

if (newValue is ICommand newCmd)
{
var pgr = new PinchGestureRecognizer();

pgr.PinchUpdated += (_, e) => { newCmd.Execute(e); };

view.GestureRecognizers.Add(pgr);
}
}
}

public static ICommand GetPinch(BindableObject view)
=> (ICommand)view.GetValue(PinchProperty);

public static void SetPinch(BindableObject view, ICommand value)
=> view.SetValue(PinchProperty, value);
}

public partial class Gesture : GestureRecognizer
Expand Down

0 comments on commit 4ae23fd

Please sign in to comment.