【问题标题】:Only allow touch after a certain time只允许在一定时间后触摸
【发布时间】:2021-10-27 23:57:20
【问题描述】:

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

【问题讨论】:

标签: swift xcode uitouch


【解决方案1】:

在整个屏幕中停止 ClickEvent

view.isUserInteractionEnabled = false

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

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

【讨论】:

    【解决方案2】:

    您可以在单击屏幕时禁用用户交互,并在 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
    } 
    

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

    【讨论】:

    • 所以我不太确定问题出在哪里。我插入了这个代码,但是当你第一次触摸时,你可以像往常一样移动所有东西(我已经不想要了),但是当你再次触摸它时,它会在此之后永远无法触摸屏幕。
    • @mrman,收到并更新了我的答案。
    • 谢谢!,不敢相信我没想到。效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    相关资源
    最近更新 更多