Prevent multiple Tap on the same UIButton
First a tip, if you only have button's calling that selector, you can change the id
to UIButton*
and drop the extra variable bCustom
.
Now, to solve your issue, you just need to ensure you turn userInteractionEnabled
back to YES
after you'd done whatever else you needed to do. Using the animation block is just an easy way because it has a completion handler built in.
You can do this simply by having selectTabControllerIndex
method do the work for you.
Something like this:
- (IBAction)btnAction:(UIButton*)sender {
sender.userInteractionEnabled = NO;
[self selectTabControllerForButton:sender];
}
- (void)selectTabControllerForButton:(UIButton*)sender {
// Whatever selectTabControllerIndex does now goes here, use sender.tag as you used index
sender.userInteractionEnabled = YES;
}
If you possibly had other code you needed to execute afterwards, you could add a completion handler to your selectTabControllerIndex
method instead and then call the completion handler. Inside that you'd include the sender.userInteractionEnabled = YES;
line. But if it's always the same code, the first way is easier and faster.
In Swift, you can also use defer
keyword, to execute a block of code that will be executed only when execution leaves the current scope.
@IBAction func btnAction(_ sender: UIButton) {
sender.isUserInteractionEnabled = false
defer {
sender.isUserInteractionEnabled = true
}
// rest of your code goes here
}
Note: This will only be helpful if the "rest of your code" is not async
, so that the execution actually leaves the current scope.
In async cases you'd need to set isUserInteractionEnabled = true
at the end of that async method.
Using userInteractionEnable=false
to prevent double tap is like using a Rocket Launcher to kill a bee.
Instead, you can use myButton.enabled=false
.Using this, you may be able to change ( if you want ) the layout of your button when it is deactivated.