GestureRecognizers.Clear(); doesn't work for iOs

AIoanAIoan Member ✭✭
edited October 2019 in Xamarin.iOS

I am trying to add and then remove a tap gesture from a layout as I need an action to be only happening once. I Add a TapGestureRecognizer to my layout with a Command and a Tapped command. When I later on remove it using myLayout.GestureRecognizers.Clear(); the tap gesture keeps happening every time I tap inside myLayout even though the GesureRecognizers is empty. This works fine on Android.
Xamarin Forms Version : 3.4.0.1008975
Xamarin iOs Runtime Version: v4.0.30319

I've created a sample project and attached the code snippet of xaml and xaml.cs below demonstrating the issue.
Not sure if this is a bug or I am just doing this wrong.

`<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="GestureRecognizerTest.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:GestureRecognizerTest">
    <StackLayout
        x:Name="TestLayout"
        HorizontalOptions="Center"
        VerticalOptions="Center">
        <Label x:Name="TestLabel" Text="Click me to Add 'A1' ONCE:" />
    </StackLayout>
</ContentPage>`



`
namespace GestureRecognizerTest
{
    public partial class MainPage : ContentPage
    {
        public ICommand OnDoSomething { get; set; }

        public MainPage()
        {
            InitializeComponent();
            OnDoSomething = new Command(() => TestLabel.Text += "A");

            AddGesture();
        }
        public void AddGesture()
        {
            TapGestureRecognizer testTapGesture;

            testTapGesture = new TapGestureRecognizer();

            testTapGesture.Tapped += ((o2, e2) =>
            {
                TestLabel.Text += "1";
                RemoveGesture();

            });
            testTapGesture.Command = OnDoSomething;

            TestLayout.GestureRecognizers.Add(testTapGesture);
        }

        public void RemoveGesture()
        {
            TestLayout.GestureRecognizers.Clear();

        }
    }
}
`

Posts

  • MommMomm USMember ✭✭✭

    Have you tried to call Remove(testTapGesture) instead? Maybe it fixes your issue for now.

  • AIoanAIoan Member ✭✭
    edited October 2019

    @Momm Thank you!! Not sure how TestLayout.GestureRecognizers.Remove(testTapGesture); is any different but it worked! :)

Sign In or Register to comment.