Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to recognize a short or long press on a screen with Xamarin.Forms?

I have an application that responds to a short tap on the screen. I do this by adding a gesture recognizer.

Is there a way that I can make it respond to either a short or a long press and have these call different methods?

like image 649
Alan2 Avatar asked Oct 20 '17 08:10

Alan2


1 Answers

You will have implement renderers for that. In case of iOS you can use UILongPressGestureRecognizer to detect a long-press action, while in case of Android, you can use GestureDetector to do the same.

Forms control

public class CustomView : ContentView
{
    public event EventHandler<EventArgs> LongPressEvent;

    public void RaiseLongPressEvent()
    {
        if (IsEnabled)
            LongPressEvent?.Invoke(this, EventArgs.Empty);
    }
}

iOS renderer

[assembly: ExportRenderer(typeof(CustomView), typeof(CustomViewRenderer))]
namespace AppNamespace.iOS
{
    public class CustomViewRenderer : ViewRenderer<CustomView, UIView>
    {
        UILongPressGestureRecognizer longPressGestureRecognizer;
        protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
        {
            longPressGestureRecognizer = longPressGestureRecognizer ??
                new UILongPressGestureRecognizer(() =>
                {
                    Element.RaiseLongPressEvent();
                });

            if (longPressGestureRecognizer != null)
            {
                if (e.NewElement == null)
                {
                    this.RemoveGestureRecognizer(longPressGestureRecognizer);
                }
                else if (e.OldElement == null)
                {
                    this.AddGestureRecognizer(longPressGestureRecognizer);
                }
            }
        }
    }
}

Android renderer

[assembly: ExportRenderer(typeof(CustomView), typeof(CustomViewRenderer))]
namespace AppNamespace.Droid
{
    public class CustomViewRenderer : ViewRenderer<CustomView, Android.Views.View>
    {
        private CustomViewListener _listener;
        private GestureDetector _detector;

        public CustomViewListener Listener
        {
            get
            {
                return _listener;
            }
        }

        public GestureDetector Detector
        {
            get
            {
                return _detector;
            }
        }

        protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                GenericMotion += HandleGenericMotion;
                Touch += HandleTouch;

                _listener = new CustomViewListener(Element);
                _detector = new GestureDetector(_listener);
            }
        }

        protected override void Dispose(bool disposing)
        {
            GenericMotion -= HandleGenericMotion;
            Touch -= HandleTouch;

            _listener = null;
            _detector?.Dispose();
            _detector = null;

            base.Dispose(disposing);
        }

        void HandleTouch(object sender, TouchEventArgs e)
        {
            _detector.OnTouchEvent(e.Event);
        }

        void HandleGenericMotion(object sender, GenericMotionEventArgs e)
        {
            _detector.OnTouchEvent(e.Event);
        }
    }

    public class CustomViewListener : GestureDetector.SimpleOnGestureListener
    {
        readonly CustomView _target;

        public CustomViewListener(CustomView s)
        {
            _target = s;
        }

        public override void OnLongPress(MotionEvent e)
        {
            _target.RaiseLongPressEvent();

            base.OnLongPress(e);
        }
    }
}

Sample Usage

<local:CustomView LongPressEvent="Handle_LongPress" />

Code-behind

void Handle_LongPressEvent(object sender, System.EventArgs e)
{
    //handle long press event here
}

You can also customize above to add a command to make it more MVVM friendly.

You can refer this link for more details regarding gesture recognizers.

like image 175
Sharada Gururaj Avatar answered Jan 03 '23 22:01

Sharada Gururaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!