Xamarin Forms Swipe Left/Swipe Right Gestures

后端 未结 8 1001
逝去的感伤
逝去的感伤 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:03

    Maybe that could help someone.

    I had a problem: there was a ContentPage with scrollview and grid in it. All I need to do is to handle swipe left/right gestures. After searching through google/stackoverflow/github, I found a Nuget package called XamarinFormsGestures. That helped me a lot. All the instruction is inside the link. There is my code:

    Vapolia.Lib.Ui.Gesture.SetSwipeLeftCommand(scrollviewgrid, 
    new Command(() => { OnLeftSwipe(); })); // What's going on when left swiped.
    Vapolia.Lib.Ui.Gesture.SetSwipeRightCommand(scrollviewgrid, 
    new Command(() => { OnRightSwipe(); })); // What's going on when right swiped.
    
    0 讨论(0)
  • 2020-12-03 03:17

    You can always have a look at this simple demo. And use it as follows:

    GestureFrame gi = new GestureFrame
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                BackgroundColor = Color.FromHex("bf3122"),
            };
    
            gi.SwipeDown += (s, e) =>
            {
                DisplayAlert("Gesture Info", "Swipe Down Detected", "OK");
                ViewModel.SampleCommand.Execute("Swipe Down Detected");
            };
    
            gi.SwipeTop += (s, e) =>
            {
                DisplayAlert("Gesture Info", "Swipe Top Detected", "OK");
                ViewModel.SampleCommand.Execute("Swipe Top Detected");
            };
    
            gi.SwipeLeft += (s, e) =>
            {
                DisplayAlert("Gesture Info", "Swipe Left Detected", "OK");
                ViewModel.SampleCommand.Execute("Swipe Left Detected");
            };
    
            gi.SwipeRight += (s, e) =>
            {
                DisplayAlert("Gesture Info", "Swipe Right Detected", "OK");
                ViewModel.SampleCommand.Execute("Swipe Right Detected");
            };
    
            this.Content = gi;
    
    0 讨论(0)
  • 2020-12-03 03:18

    If you're comfortable with paying for a third-party library (and you're using Xamarin Forms, so that's a good possibility), MR.Gestures supports all touch gestures on all Xamarin.Forms Views. I've used it successfully and been really happy with it. It costs a very reasonable €10 and has excellent documentation.

    If you're one of the many people who are disappointed that touch gestures aren't supported in Xamarin Forms, consider voting for this suggestion at UserVoice.

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

    FYI

    SwipeView is available in Xamarin.Forms 4.4.

    The SwipeView class also defines four events:

    SwipeStarted is fired when a swipe starts. The SwipeStartedEventArgs object that accompanies this event has a SwipeDirection property, of type SwipeDirection.

    SwipeChanging is fired as the swipe moves. The SwipeChangingEventArgs object that accompanies this event has a SwipeDirection property, of type SwipeDirection, and an Offset property of type double.

    SwipeEnded is fired when a swipe ends. The SwipeEndedEventArgs object that accompanies this event has a SwipeDirection property, of type SwipeDirection.

    CloseRequested is fired when the swipe items are closed. In addition, SwipeView defines a Close method, which closes the swipe items.

    For more information about some new Xamarin Forms features visit this link.

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

    Building off of @Ranjith Kumar's solution, I came up with the following:

    public delegate void SwipedEventHandler(ISwipeListener sender, SwipedEventArgs e);
    
    public class SwipedEventArgs : EventArgs
    {
        readonly double _x;
        public double X => _x;
    
        readonly double _y;
        public double Y => _y;
    
        readonly View _view;
        public View View => _view;
    
        public SwipedEventArgs(View view, double x, double y)
        {
            _view = view;
            _x = x;
            _y = y;
        }
    }
    
    public interface ISwipeListener
    {
        event SwipedEventHandler SwipedDown;
    
        event SwipedEventHandler SwipedLeft;
    
        event SwipedEventHandler SwipedNothing;
    
        event SwipedEventHandler SwipedRight;
    
        event SwipedEventHandler SwipedUp;
    
        double TotalX
        {
            get;
        }
    
        double TotalY
        {
            get;
        }
    }
    
    public class SwipeListener : PanGestureRecognizer, ISwipeListener
    {
        public event SwipedEventHandler SwipedDown;
    
        public event SwipedEventHandler SwipedLeft;
    
        public event SwipedEventHandler SwipedNothing;
    
        public event SwipedEventHandler SwipedRight;
    
        public event SwipedEventHandler SwipedUp;
    
        double _totalX = 0, _totalY = 0;
    
        public double TotalX => _totalX;
    
        public double TotalY => _totalY;
    
        readonly View _view;
    
        public SwipeListener(View view) : base()
        {
            _view = view;
            _view.GestureRecognizers.Add(this);
            PanUpdated += OnPanUpdated;
        }
    
        void OnPanUpdated(object sender, PanUpdatedEventArgs e)
        {
            switch (e.StatusType)
            {
                case GestureStatus.Running:
                    try
                    {
                        _totalX = e.TotalX;
                        _totalY = e.TotalY;
                    }
                    catch (Exception exception)
                    {
                        Debug.WriteLine(exception.Message);
                    }
                    break;
    
                case GestureStatus.Completed:
                    if (_totalX < 0 && Math.Abs(_totalX) > Math.Abs(_totalY))
                    {
                        OnSwipedLeft(_totalX, _totalY);
                    }
                    else if (_totalX > 0 && _totalX > Math.Abs(_totalY))
                    {
                        OnSwipedRight(_totalX, _totalY);
                    }
                    else if (_totalY < 0 && Math.Abs(_totalY) > Math.Abs(_totalX))
                    {
                        OnSwipedUp(_totalX, _totalY);
                    }
                    else if (_totalY > 0 && _totalY > Math.Abs(_totalX))
                    {
                        OnSwipedDown(_totalX, _totalY);
                    }
                    else OnSwipedNothing(_totalX, _totalY);
                    break;
    
            }
        }
    
        protected virtual void OnSwipedDown(double x, double y)
            => SwipedDown?.Invoke(this, new SwipedEventArgs(_view, x, y));
    
        protected virtual void OnSwipedLeft(double x, double y)
            => SwipedLeft?.Invoke(this, new SwipedEventArgs(_view, x, y));
    
        protected virtual void OnSwipedNothing(double x, double y)
            => SwipedNothing?.Invoke(this, new SwipedEventArgs(_view, x, y));
    
        protected virtual void OnSwipedRight(double x, double y)
            => SwipedRight?.Invoke(this, new SwipedEventArgs(_view, x, y));
    
        protected virtual void OnSwipedUp(double x, double y)
            => SwipedUp?.Invoke(this, new SwipedEventArgs(_view, x, y));
    }
    

    The downside is you can't do anything while the swipe is performed, only after.

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

    Xamarin.Forms has introduced SwipeGestureRecognizer :

    <BoxView Color="Teal" ...>
        <BoxView.GestureRecognizers>
            <SwipeGestureRecognizer Direction="Left" Swiped="OnSwiped"/>
        </BoxView.GestureRecognizers>
    </BoxView>
    
    0 讨论(0)
提交回复
热议问题