In my last question I learned to put a timer on the game so that after the start button was clicked, the user would have to wait a few seconds to be able to touch the screen. That code looks like this:
self.view?.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
self.view?.isUserInteractionEnabled = true
}
I have kept the last post up for learning purposes for others. This time I am wondering how you would go about not allowing touch at all until a button is pressed! I switched the true statement above to "false", so the user cannot touch when first on the game. However, I want the user to be able to click the start button to then allow all other touch during the game. Thanks!
2
Answers
A very simple way you could do this would be to create a subclass of UIView that overrides
func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
.Then you simply check to see if the button has been pressed before forwarding events to other subviews.
My solution is to place an invisible view in front of the entire interface and use hit-test munging so that only one view behind it (
self.passthruView
, your button) is touchable:So as long as MyView is sitting (invisibly) in front of the whole interface, only the button can be touched. Then you take away MyView and the whole interface becomes touchable again.