Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Catching own gesture on custom control.

$
0
0

Hi All,

According to this article. I am going to add Fling gesture to the my custom control (Image).

public class MyImage : Image
    {
        public MyImage ()
        {
            Aspect = Aspect.AspectFit;
        }
    }

Renderer:

`public class MyImageRenderer : ImageRenderer
{
private readonly MyGestureListener _listener;
private readonly GestureDetector _detector;

    public MyImageRenderer()
    {
        _listener = new MyGestureListener();
        _detector = new GestureDetector(_listener);
    }

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

        if (e.NewElement == null)
        {
            this.GenericMotion -= HandleGenericMotion;
            this.Touch -= HandleTouch;
        }

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

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

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

and MyGestureListener:

public class MyGestureListener : GestureDetector.SimpleOnGestureListener
    {
        public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {
            Console.WriteLine("OnFling");
            return base.OnFling(e1, e2, velocityX, velocityY);
        }
    }

When I am flinging on the image in the Output I can see "OnFling" but how to deal with this in my view? I want to achieve something like this:

MyImage im = new MyImage(){};
 im.Fling += (s,e) =>
{
 Console.WriteLine("Fling from view");
}

Thanks, for your help.


Viewing all articles
Browse latest Browse all 58056

Trending Articles