How to best implement a swipe gesture recognizer in Xamarin Forms for windows phone (8.1 & 10) platform?
I see a lot of examples for renderers that do that for Android and iOS platform. But not for WinRT or UWP.
How to best implement a swipe gesture recognizer in Xamarin Forms for windows phone (8.1 & 10) platform?
I see a lot of examples for renderers that do that for Android and iOS platform. But not for WinRT or UWP.
I see a lot of examples for renderers that do that for Android and iOS platform. But not for WinRT or UWP.
Currently, there is no such "SwipeGestureRecognizer" api for Xamarin.Forms. But you could custom SwipeGestureRecognizer
base on PanGestureRecognizer
. I have written the following code for simulating "SwipeGestureRecognizer". But the threshold I used is just for testing and not the real one, you can modify the threshold based on your requirement.
public enum SwipeDeriction
{
Left = 0,
Rigth,
Above,
Bottom
}
public class SwipeGestureReconginzer : PanGestureRecognizer
{
public delegate void SwipeRequedt(object sender, SwipeDerrictionEventArgs e);
public event EventHandler<SwipeDerrictionEventArgs> Swiped;
public SwipeGestureReconginzer()
{
this.PanUpdated += SwipeGestureReconginzer_PanUpdated;
}
private void SwipeGestureReconginzer_PanUpdated(object sender, PanUpdatedEventArgs e)
{
if (e.TotalY > -5 | e.TotalY < 5)
{
if (e.TotalX > 10)
{
Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Rigth));
}
if (e.TotalX < -10)
{
Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Left));
}
}
if (e.TotalX > -5 | e.TotalX < 5)
{
if (e.TotalY > 10)
{
Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Bottom));
}
if (e.TotalY < -10)
{
Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Above));
}
}
}
}
public class SwipeDerrictionEventArgs : EventArgs
{
public SwipeDeriction Deriction { get; }
public SwipeDerrictionEventArgs(SwipeDeriction deriction)
{
Deriction = deriction;
}
}
MainPage.xaml.cs
var swipe = new SwipeGestureReconginzer();
swipe.Swiped += Tee_Swiped;
TestLabel.GestureRecognizers.Add(swipe);
private void Tee_Swiped(object sender, SwipeDerrictionEventArgs e)
{
switch (e.Deriction)
{
case SwipeDeriction.Above:
{
}
break;
case SwipeDeriction.Left:
{
}
break;
case SwipeDeriction.Rigth:
{
}
break;
case SwipeDeriction.Bottom:
{
}
break;
default:
break;
}
}