在一定时间后才允许触摸



对于我正在创建的游戏,我在开始按钮被按下时设置了一些动画。然而,触摸屏幕会干扰这个动画。是否有任何方法可以让用户无法与屏幕交互,直到(让我们说10秒)开始按钮后点击?下面是当前用户触摸的代码:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch in touches {

let location = touch.location(in: self)
player.position.x = location.x
player.position.y = -300//location.y

}
}

Stop ClickEvent

view.isUserInteractionEnabled = false

继续ClickEvent一段时间后,即。: 10秒

DispatchQueue.main.asyncAfter(deadline: .now() + 10) { 
self.view.isUserInteractionEnabled = true 
}

您可以在点击屏幕时禁用用户交互,并在10秒后启用它-

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch in touches {

let location = touch.location(in: self)
player.position.x = location.x
player.position.y = -300//location.y

}
self.view.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
self.view.isUserInteractionEnabled = true
} 
}

如果你想在第一次点击开始按钮后10秒内禁用用户交互,那么复制下面的代码

self.view.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
self.view.isUserInteractionEnabled = true
} 

最新更新