Quantcast
Channel: Prevent multiple Tap on the same UIButton - Stack Overflow
Viewing all articles
Browse latest Browse all 11

Answer by ingconti for Prevent multiple Tap on the same UIButton

$
0
0

it seems that under iOS 14.x it will happen automatically when You tap.

I have written small demo app with a nav controller, a controller of class "ViewController" with a button invoking an action "pushIt".

(see code)I have set Storyboard ID to a separated controller to "ColoredVCID" and added a global counter, just to see...

Long way SHORT: it seems working correctly.

//  compulsiveTouch////  Created by ing.conti on 03/08/21.   import UIKit    fileprivate var cont = 0    class ViewController: UIViewController {        @IBAction func pushIt(_ sender: Any) {            cont+=1            print(cont)            let storyboard = UIStoryboard(name: "Main", bundle: nil)            let vc = storyboard.instantiateViewController(withIdentifier: "ColoredVCID")            self.navigationController!.present(vc, animated: true)            // OR:            self.navigationController!.pushViewController(vc, animated: true)        }    }

In PAST days I usually did:

   @objc func pushItOLD(_sender: Any){        // prevent compulsive touch:        self.setButtonActive(btn: self.pushBtn!, active: false)        // now re-eanble it... after 1 second:        let when = DispatchTime.now() + 1        DispatchQueue.main.asyncAfter(deadline: when, execute: { () -> Void in            self.setButtonActive(btn: self.pushBtn!, active: true)        })    }    func setButtonActive(btn: UIButton?,  active: Bool){        guard let btn = btn else{            return        }        btn.isEnabled = active        btn.alpha = (active ? 1 : 0.5)    }

that CAN BE very useful nowadays if your button for example invokes a network request... to prevent double calls.

(I added some cosmetics to use alpha.. to let user see it as "disabled" ..)


Viewing all articles
Browse latest Browse all 11

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>