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;
}
}
}
Try to pass a UILongPressGestureRecognizer
as parameter to LongPress
method , and change its State
when you want to stop it .
public void LongPress(UILongPressGestureRecognizer g)
{
if (a == true)
{
a = false;
}
else
{
a = true;
}
// stop recognizing long press gesture here
g.State = UIGestureRecognizerState.Ended;
}