How to determine location of a tap in Xamarin Forms?
Asked Answered
W

3

10

In Xamarin Forms how do I find out the location of a tap (within an image, say)?My code is:

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += OnTapGestureRecognizerTapped;
image.GestureRecognizers.Add(tapGestureRecognizer);
...
...
void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{ ... }

args.Parameter returns null (for iOS). I did not find any docs for Parameter. In the handler I tried changing EventArgs to TappedEventArgs but then did not compile. Also tried casting args to TappedEventArgs but that did not change anything.

Wildon answered 7/6, 2016 at 12:48 Comment(4)
do you want to know (X,Y) of the tap ?Antiquity
Yes, exactly, I want to know the (x,y). :)Wildon
I'm not sure if xamarin forms gestures returns the position of the tap, if not, you can create custom renderer of an image or a content view and add tap gesture directly in the custom renderer and returns to forms the position var pt = p.LocationInView (this); PostionX = pt.X; PostionY = pt.Y; I made something like this to return te X, Y using longpressAntiquity
Thanks Ricardo. Was just hoping to avoid the extra steps.Wildon
D
5

After days of searching for the easiest solution, I finally found it (actually it's quite a shame that there is no build way to get the tap coordinates...). Anyway here is what you're looking for: https://learn.microsoft.com/en-gb/xamarin/xamarin-forms/app-fundamentals/effects/touch-tracking

with examples here:

https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/effects-touchtrackingeffect/

I already tried it within my app and all you need is

  • TouchActionEventArgs.cs

  • TouchActionEventHandler.cs

  • TouchActionType.cs

  • TouchEffect.cs

to add to the project (only rename the namespace) and than add TouchEffect.cs to the .Android section (again rename all names to correspond with the name of your app).

And there you go, happy coding ;)

Doublecheck answered 13/8, 2020 at 9:43 Comment(0)
O
3

At the moment of writing this answer it seems that there is no way of receiving the coordinates of the tap in XamarinForms without platform specific code. There are third party libraries (mentioned here) which provide this information.

Oxyacetylene answered 27/10, 2016 at 11:50 Comment(1)
2022 update - Xamarin Forms has TouchEffects that allows this. More info here: learn.microsoft.com/en-us/xamarin/xamarin-forms/…Aurochs
B
1

In order to get the position of touch, you have to create a custom renderer for page. Here's an example :

public class ExtendedContentPageRenderer : PageRenderer
{
    private ExtendedContentPage _thePage;
    private UITapGestureRecognizer _tapGestureRecognizer;

    public ExtendedContentPageRenderer()
    {
        _tapGestureRecognizer = new UITapGestureRecognizer(a => UITapGestureRecognizerHandler(a))
        {
            ShouldRecognizeSimultaneously = (recognizer, gestureRecognizer) => true,
            ShouldReceiveTouch = (recognizer, touch) => true,
        };
        _tapGestureRecognizer.DelaysTouchesBegan = _tapGestureRecognizer.DelaysTouchesEnded = _tapGestureRecognizer.CancelsTouchesInView = false;
    }

    private void UITapGestureRecognizerHandler(UITapGestureRecognizer gestureRecog)
    {
        if (gestureRecog.State == UIGestureRecognizerState.Ended)
        {
            var endPos = gestureRecog.LocationOfTouch(0, gestureRecog.View);

            _thePage.TriggerTouchDown(endPos.X, endPos.Y);
        }
    }

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        if (e.OldElement != null)
        {
            View.RemoveGestureRecognizer(_tapGestureRecognizer);
            _thePage = null;
        }

        if (e.NewElement != null)
        {
            _thePage = e.NewElement as ExtendedContentPage;
            View.AddGestureRecognizer(_tapGestureRecognizer);
        }

        base.OnElementChanged(e);
    }
}

As you can see, I've created a custom page for propagating back the touch event to the main page. In the page you could then see if the touch coordinates passed lies withing the bounds of the image view in consideration.

Brothel answered 17/3, 2020 at 9:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.