0

I'm currently working with "Press and hold" on a button, here is my code:

public override void AwakeFromNib()
{
    base.AwakeFromNib();

    longp = new UILongPressGestureRecognizer(LongPress);
    button.AddGestureRecognizer(longp);
}

public void LongPress()
{
    if (a == true)
    {
       a = false;
    }
    else
    {
       a = true;
    }
    // stop recognizing long press gesture here
}

The problem is since I'm running a toggle method to change a value, all it does is just spamming the method LongPress, how to I cancel or stop the holding after changing the value?

Update

I've managed to get it work, here is my code example:

public void LongPress(UILongPressGestureRecognizer g)
{
    if (g.State == UIGestureRecognizerState.Began)
    {
        if (a == true)
        {
            a = false;
        }
        else
        {
            a = true;
        }
    }
}

1 Answer 1

0

Try to pass a UILongPressGestureRecognizer as parameter to LongPress method , and change its State when you want to stop it .

sample code

public void LongPress(UILongPressGestureRecognizer g)
{
    if (a == true)
    {
        a = false;
    }
    else
    {
        a = true;
    }

    // stop recognizing long press gesture here
    g.State = UIGestureRecognizerState.Ended; 
}
1
  • Thank you for the advice, I've managed to get it work. I've updated my code example. Sep 15, 2021 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.