Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel UIButton touch while touching it

I have a situation where a user would be touching a UIButton and while they might already be touching it, I need to be able to cancel it or cause it to fail.

I have tried to use button.enabled and userInteractionEnabled, but neither one works on the fly. Both have to be set before the touch begins.

Can I cause the button to fail after and while it is being touched?

like image 677
daveMac Avatar asked Feb 08 '13 15:02

daveMac


1 Answers

You need to register two actions for your button, one for touch down and one for touch up. In touch down, you can decide if you want to cancel; and in touch up, write the actual logic for button press.

[button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown]; 

- (IBAction)touchDown:(id)sender
{
    UIButton *b = (UIButton*)sender;

    // Call this if you wish to cancel the event tracking
    [b cancelTrackingWithEvent:nil];
}

For reference, see the documentation of -cancelTrackingWithEvent: in UIControl.

like image 113
Sohaib Avatar answered Sep 27 '22 18:09

Sohaib