Xamarin Forms Swipe Left/Swipe Right Gestures

后端 未结 8 1002
逝去的感伤
逝去的感伤 2020-12-03 02:46

I want to preface this by saying I\'m completely new to mobile development, Xamarin, C#, .Net.

I\'m working on creating a mobile app using Xamarain Forms and have ru

相关标签:
8条回答
  • 2020-12-03 03:25

    No need third party libraries.. No need to pay.. Just add these two classes & Implement 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
    

    Sample project --> https://github.com/rranjithkumar100/Xamarin-Swipe-Library

    0 讨论(0)
  • 2020-12-03 03:26

    You can use the NuGet package "XamarinFormsGesture" of Vapolia (doc available here). It's free and easy to use.

    Available on iOS and Android but it only works on physical devices (not on simulator).

    0 讨论(0)
提交回复
热议问题