skip to Main Content

For the game I am creating, I have a little animation of sorts at the beginning when the start button is pressed. However being able to touch the screen interferes with this animation. Is there any way I can have the user not be able to interact with the screen until (lets say 10 seconds) after the start button is clicked? Here is my code for the current users touch:

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
        
    }
}

2

Answers


  1. Stop ClickEvent in entire screen.

    view.isUserInteractionEnabled = false
    

    Continue ClickEvent after some time, i.e.: 10 Sec.

    DispatchQueue.main.asyncAfter(deadline: .now() + 10) { 
        self.view.isUserInteractionEnabled = true 
    }
    
    Login or Signup to reply.
  2. You can disable user interaction while clicking on the screen and enable it after 10 second as-

    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
        } 
    }
    

    If you want to disable user interaction for 10 seconds after clicking the start button for the first time, then copy this code

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

    and paste it inside your Start button action function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search