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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With