Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumberOfTapsRequired doesn't work for > 2

I have Xamarin Forms project and on my page there is this code:

<Image Source="genea_login_logo.png">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding LogoCommand}" NumberOfTapsRequired="2" />
    </Image.GestureRecognizers>
</Image>

It works as expected. However when I change NumberOfTapsRequired from 2 to 5, it doesn't work any more. Is this behaviour expected? Is it possible to implement 5-click command?

Edit: This is only on Android.

like image 550
Uros Avatar asked May 16 '17 13:05

Uros


1 Answers

TapGestureRecognizer with more than 2 NumberOfTapsRequired doesn't work, actually, in Android. You can implement some kind of logic to achieve this effect.

In XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Demo.Views.MainPage"             
             Title="MainPage">
    <Grid HorizontalOptions="Center" VerticalOptions="Center">       

        <Image Source="icon.png">
            <Image.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_OnTapped"/>
            </Image.GestureRecognizers>
        </Image>

    </Grid>
</ContentPage>

Code behind....

    private DateTime? LastTap = null;
    private byte NumberOfTaps = 0;

    private const int NumberOfTapsRequired = 3;
    private const int ToleranceInMs = 1000;

    private async void TapGestureRecognizer_OnTapped(object sender, EventArgs e)
    {
        if (LastTap ==  null || (DateTime.Now - LastTap.Value).TotalMilliseconds < ToleranceInMs)
        {
            if (NumberOfTaps == (NumberOfTapsRequired - 1))
            {

                await App.Current.MainPage.DisplayAlert("Hi", "Hi from Gesture", "Ok");

                NumberOfTaps = 0;
                LastTap = null;
                return;
            }
            else
            {
                NumberOfTaps++;
                LastTap = DateTime.Now;
            }
        }
        else
        {
            NumberOfTaps=1;
            LastTap = DateTime.Now;
        }
    }
like image 128
Jesus Angulo Avatar answered Sep 21 '22 21:09

Jesus Angulo