Reputation: 1136
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.
Upvotes: 10
Views: 9236
Reputation: 129
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 ;)
Upvotes: 5
Reputation: 2974
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.
Upvotes: 1
Reputation: 777
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.
Upvotes: 3