Skip to content
rranjithkumar100 edited this page Aug 2, 2017 · 1 revision

No need third party libraries.. No need to pay.. Just add these classes & get your swipe listeners

Step 1: Copy paste these two classes

SwipeListener.cs

using System;
using Xamarin.Forms;

namespace SwipeLib
{
public class SwipeListener : PanGestureRecognizer
{
	private ISwipeCallBack mISwipeCallback;
	private double translatedX = 0, translatedY = 0;

	public SwipeListener(View view, ISwipeCallBack iSwipeCallBack)
	{
		mISwipeCallback = iSwipeCallBack;
		var panGesture = new PanGestureRecognizer();
		panGesture.PanUpdated += OnPanUpdated;
		view.GestureRecognizers.Add(panGesture);
	}

	void OnPanUpdated(object sender, PanUpdatedEventArgs e)
	{

		View Content = (View)sender;

		switch (e.StatusType) {

			case GestureStatus.Running:

				try {
					translatedX = e.TotalX;
					translatedY = e.TotalY;
				} catch (Exception err) {
					System.Diagnostics.Debug.WriteLine("" + err.Message);
				}
				break;

			case GestureStatus.Completed:

				System.Diagnostics.Debug.WriteLine("translatedX : " + translatedX);
				System.Diagnostics.Debug.WriteLine("translatedY : " + translatedY);

				if (translatedX < 0 && Math.Abs(translatedX) > Math.Abs(translatedY)) {
					mISwipeCallback.onLeftSwipe(Content);
				} else if (translatedX > 0 && translatedX > Math.Abs(translatedY)) {
					mISwipeCallback.onRightSwipe(Content);
				} else if (translatedY < 0 && Math.Abs(translatedY) > Math.Abs(translatedX)) {
					mISwipeCallback.onTopSwipe(Content);
				} else if (translatedY > 0 && translatedY > Math.Abs(translatedX)) {
					mISwipeCallback.onBottomSwipe(Content);
				} else {
					mISwipeCallback.onNothingSwiped(Content);
				}

				break;

		}
	}

}
}

ISwipeCallBack.cs

using System;
using Xamarin.Forms;
namespace SwipeLib
{  
public interface ISwipeCallBack
{

	void onLeftSwipe(View view);
	void onRightSwipe(View view);
	void onTopSwipe(View view);
	void onBottomSwipe(View view);
	void onNothingSwiped(View view);
}
}

Step 2: From your Xamarin forms pass the view & also interface obj. Then you get result

In my case I pass the label

 SwipeListener swipeListener = new SwipeListener(lbl_swipe, this);

Step 3: Implement the ISwipeCallBack interface

public partial class SwipeLibPage : ContentPage, ISwipeCallBack
Clone this wiki locally