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 ?
You could try to have both buttons (button0 and button1) as
IBOutletand assign thetouchUpInsideevent to fire a button0Press/button1Press function. And then you could potentiallyenablebutton1after x seconds(1 second in my example) that button0 has been pressed. And as well viceversa.