Search code examples
swiftsprite-kittvossiri-remote

tvos swift spritekit - UITapGestureRecognizer not responding


I have not been able to get this gesture to respond, at all.

    self.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(self.movePlayer(_:)))
    tap.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue)]
    view?.addGestureRecognizer(tap)

and the function:

func movePlayer(_ sender: UITapGestureRecognizer){
    player.run(SKAction.moveBy(x: 200, y: 0, duration: 10))
    print("Right!")
}

nothing at all happens when pressing the play button on the simulator remote. What am I missing?


Solution

  • The tap gesture recogniser code looks fine. Where are you adding it, is it in didMoveToView?

    I was recently playing around with the new iOS 10

    override func sceneDidLoad() { }
    

    method and I noticed that gesture recognisers added there do not work.

    This works in the simulator

     class GameScene: SKScene, SKPhysicsContactDelegate {
    
          override func didMove(to view: SKView) {
    
              self.isUserInteractionEnabled = true
              let tap = UITapGestureRecognizer(target: self, action: #selector(self.movePlayer(_:)))
              tap.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue)]
              view.addGestureRecognizer(tap)
         }
    
         func movePlayer(_ sender: UITapGestureRecognizer){
             print("Right!")
    
         }
     }
    

    Hope this helps