I'm working with sprite kit and if the user touches the screen, the actions within
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
}
}
are carried out. While they're being carried out, however, the user can still tap the screen and the app tries to run the actions again.
How do I disable touch interaction/the actions within the touch func while the actions are running?
If you want to disable the user interaction as a view, it will be same just remove the button name and add the view's name. If for some reasons, you want to enable interaction, just change "false" to "true".
Put your view to be "tap disabled" into a Group then apply the modifier . allowsHitTesting(false) to disable interaction with any view inside the group. To enable interaction switch the modifier to true.
SwiftUI lets us stop a view from receiving any kind of taps using the allowsHitTesting() modifier. If hit testing is disallowed for a view, any taps automatically continue through the view on to whatever is behind it.
Try to get the view from the touch object and then dissable the user interaction on it.
touch.view.isUserInteractionEnabled = false
To disable user interaction app-wide, use:
UIApplication.shared.beginIgnoringInteractionEvents()
UIApplication.shared.endIgnoringInteractionEvents()
(as of Swift 5 this is deprecated)
In Swift 3.0 is:
self.view.isUserInteractionEnabled = false
You can use boolean class variable to stop interaction while method is performing, and after that you can just change value of boolean, at the end of the method.
Use UIApplication.shared.beginIgnoringInteractionEvents()
at the end of the first method, then change value of boolean and then use another method with start line UIApplication.shared.endIgnoringInteractionEvents()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With