Swift:如何在执行触摸操作时禁用用户交互?

时间:2023-01-23 15:13:36

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?

如何在操作运行时禁用触摸交互/触摸功能中的操作?

4 个解决方案

#1


17  

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

尝试从触摸对象获取视图,然后在其上禁用用户交互。

touch.view.userInteractionEnabled = false

#2


15  

In Swift 3.0 is:

在Swift 3.0中是:

self.view.isUserInteractionEnabled = false

#3


1  

To disable user interaction app-wide, use:

要在应用范围内禁用用户互动,请使用:

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

#4


0  

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

在第一个方法的末尾使用UIApplication.shared.beginIgnoringInteractionEvents(),然后更改boolean的值,然后使用另一个带有起始行UIApplication.shared.endIgnoringInteractionEvents()的方法。

#1


17  

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

尝试从触摸对象获取视图,然后在其上禁用用户交互。

touch.view.userInteractionEnabled = false

#2


15  

In Swift 3.0 is:

在Swift 3.0中是:

self.view.isUserInteractionEnabled = false

#3


1  

To disable user interaction app-wide, use:

要在应用范围内禁用用户互动,请使用:

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

#4


0  

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

在第一个方法的末尾使用UIApplication.shared.beginIgnoringInteractionEvents(),然后更改boolean的值,然后使用另一个带有起始行UIApplication.shared.endIgnoringInteractionEvents()的方法。