Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: how to disable user interaction while touch action is being carried out?

Tags:

swift

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?

like image 907
blue Avatar asked Mar 16 '15 01:03

blue


People also ask

How do you stop a user interaction in Swift?

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".

How to disable user interaction button in SwiftUI?

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.

How do I turn off View in SwiftUI?

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.


4 Answers

Try to get the view from the touch object and then dissable the user interaction on it.

touch.view.isUserInteractionEnabled = false
like image 76
Suhas Aithal Avatar answered Oct 14 '22 12:10

Suhas Aithal


To disable user interaction app-wide, use:

UIApplication.shared.beginIgnoringInteractionEvents()
UIApplication.shared.endIgnoringInteractionEvents()

(as of Swift 5 this is deprecated)

like image 24
Jan Erik Schlorf Avatar answered Oct 14 '22 10:10

Jan Erik Schlorf


In Swift 3.0 is:

self.view.isUserInteractionEnabled = false
like image 34
Javier Calatrava Llavería Avatar answered Oct 14 '22 10:10

Javier Calatrava Llavería


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().

like image 2
Vladimir Sukanica Avatar answered Oct 14 '22 10:10

Vladimir Sukanica