0

There is a simple UIViewController with two buttons, every time a button is pressed, a full screen view will be added to the viewcontorller's view, as its size is the same as the viewcontroller.

We know that if a view is overlapped its parentview, the parentview cannot respond any event.

But if I press the button0 and then very fast press button1, the view is added to the viewcontorller and the second button's touch event is also evoked.

Here is my code:

@IBAction func button0Pressed(_ sender: Any) {
        print(#function)
        let view0 = UIView(frame: view.bounds)
        view0.backgroundColor = UIColor.gray
        let tap0 = UITapGestureRecognizer(target: self, action: #selector(tap0(_:)))
        view0.addGestureRecognizer(tap0)
        view.addSubview(view0)
    }

    @objc func tap0(_ gestureRecognizer: UITapGestureRecognizer) {
        print(#function)
        let view = gestureRecognizer.view
        view?.removeFromSuperview()
    }

    @IBAction func button1Pressed(_ sender: Any) {
        print(#function)
        let view1 = UIView(frame: view.bounds)
        view1.backgroundColor = UIColor.lightGray
        let tap1 = UITapGestureRecognizer(target: self, action: #selector(tap1(_:)))
        view1.addGestureRecognizer(tap1)
        view.addSubview(view1)
    }


    @objc func tap1(_ gestureRecognizer: UITapGestureRecognizer) {
        print(#function)
        let view = gestureRecognizer.view
        view?.removeFromSuperview()
    }

Out Put:

button0Pressed button1Pressed

So, How can I prevent the viewcontrollers quick touch event after the button0 is pressed ?

Sayaka
  • 21
  • 2

1 Answers1

0

You could try to have both buttons (button0 and button1) as IBOutlet and assign the touchUpInside event to fire a button0Press/button1Press function. And then you could potentially enable button1 after x seconds (1 second in my example) that button0 has been pressed. And as well viceversa.

@IBOutlet weak var button0: UIButton!
@IBOutlet weak var button1: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()
    button0.addTarget(self, action: #selector(button0Pressed), for: .touchUpInside)
    button1.addTarget(self, action: #selector(button1Pressed), for: .touchUpInside)
}

@objc func button0Pressed() {
    print(#function)
    let view0 = UIView(frame: view.bounds)
    view0.backgroundColor = UIColor.gray
    let tap0 = UITapGestureRecognizer(target: self, action: #selector(tap0(_:)))
    view0.addGestureRecognizer(tap0)
    view.addSubview(view0)

    // Enable button1 after 1 second
    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000)) {
        self.button1.isEnabled = true
    }
}

@objc func button1Pressed() {
    print(#function)
    let view1 = UIView(frame: view.bounds)
    view1.backgroundColor = UIColor.lightGray
    let tap1 = UITapGestureRecognizer(target: self, action: #selector(tap1(_:)))
    view1.addGestureRecognizer(tap1)
    view.addSubview(view1)

    // Enable button0 after 1 second
    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000)) {
        self.button0.isEnabled = true
    }
}
denis_lor
  • 6,212
  • 4
  • 31
  • 55