Last active
June 24, 2019 11:03
-
-
Save hamid-shaikh/211286817f439584681d9c38cfe2f954 to your computer and use it in GitHub Desktop.
Xamarin.Forms Attach TapToCommandBehavior to any control and make it Tappable with Animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using System.Windows.Input; | |
using Xamarin.Forms; | |
public class TapToCommandBehavior : Behavior | |
{ | |
private readonly TapGestureRecognizer tapGestureRecognizer; | |
private View AttachedToView; | |
public TapToCommandBehavior() | |
{ | |
tapGestureRecognizer = new TapGestureRecognizer { Command = GetAnimatedCommand() }; | |
} | |
#region TapCommandProperty | |
public static readonly BindableProperty TapCommandProperty = | |
BindableProperty.Create(nameof(TapCommand), | |
typeof(ICommand), | |
typeof(View), | |
null); | |
public ICommand TapCommand | |
{ | |
get => (ICommand)GetValue(TapCommandProperty); | |
set => SetValue(TapCommandProperty, value); | |
} | |
#endregion | |
#region TapCommandParameterProperty | |
public static readonly BindableProperty TapCommandParameterProperty = | |
BindableProperty.Create(nameof(TapCommandParameter), | |
typeof(object), | |
typeof(View), | |
null); | |
public object TapCommandParameter | |
{ | |
get => GetValue(TapCommandParameterProperty); | |
set => SetValue(TapCommandParameterProperty, value); | |
} | |
#endregion | |
#region AnimateOnTapProperty | |
public static readonly BindableProperty AnimateOnTapProperty = | |
BindableProperty.Create(nameof(AnimateOnTap), | |
typeof(bool), | |
typeof(View), | |
true); | |
public bool AnimateOnTap | |
{ | |
get => (bool)GetValue(AnimateOnTapProperty); | |
set => SetValue(AnimateOnTapProperty, value); | |
} | |
#endregion | |
#region ViewToAnimateOnTapProperty | |
public static readonly BindableProperty ViewToAnimateOnTapProperty = | |
BindableProperty.Create(nameof(ViewToAnimateOnTap), | |
typeof(View), | |
typeof(View), | |
null); | |
public View ViewToAnimateOnTap | |
{ | |
get => (View)GetValue(ViewToAnimateOnTapProperty); | |
set => SetValue(ViewToAnimateOnTapProperty, value); | |
} | |
#endregion | |
#region Behavior Events | |
protected override void OnAttachedTo(BindableObject bindable) | |
{ | |
base.OnAttachedTo(bindable); | |
if (bindable is View control) | |
{ | |
AttachedToView = control; | |
control.BindingContextChanged += BindableOnBindingContextChanged; | |
control.GestureRecognizers.Add(tapGestureRecognizer); | |
} | |
} | |
private void BindableOnBindingContextChanged(object sender, EventArgs eventArgs) | |
{ | |
BindingContext = (sender as View).BindingContext; | |
} | |
protected override void OnDetachingFrom(BindableObject bindable) | |
{ | |
base.OnDetachingFrom(bindable); | |
bindable.BindingContextChanged -= BindableOnBindingContextChanged; | |
} | |
#endregion | |
#region Public Methods | |
public ICommand GetAnimatedCommand() | |
{ | |
return new Command(async () => | |
{ | |
if (ViewToAnimateOnTap != null && AnimateOnTap) | |
{ | |
ViewToAnimateOnTap.AnchorX = ViewToAnimateOnTap.AnchorY = 0.48; | |
await ViewToAnimateOnTap.ScaleTo(0.8, 50, Easing.Linear); | |
await Task.Delay(100); | |
await ViewToAnimateOnTap.ScaleTo(1, 50, Easing.Linear); | |
} | |
else if (AttachedToView != null && AnimateOnTap) | |
{ | |
AttachedToView.AnchorX = AttachedToView.AnchorY = 0.48; | |
await AttachedToView.ScaleTo(0.8, 50, Easing.Linear); | |
await Task.Delay(100); | |
await AttachedToView.ScaleTo(1, 50, Easing.Linear); | |
} | |
TapCommand?.Execute(TapCommandParameter); | |
}); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please find the example usage in below link.
https://gist.github.com/hamid-shaikh/62778ddd4e97b994e5991856bae6ba01