只允许在一定时间后触摸

Only allow touch after a certain time

对于我正在创建的游戏,当按下开始按钮时,我在开始时有一些类似的动画。但是,能够触摸屏幕会干扰此动画。有什么办法可以让用户在单击开始按钮后(比方说 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
        
    }
}

停止全屏点击事件

view.isUserInteractionEnabled = false

一段时间后继续单击事件即: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
} 

将其粘贴到您的“开始”按钮操作函数中。