2

How can I access the play/pause button with the Siri remote and override the menu button? I am currently using this, but it is not working for me. My program crashes when I use this code but only when I call it four example pressing the pause button The coders is currently positioned below didMoveToView next to touchesBegan

let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)
Ferdinand Lösch
  • 185
  • 3
  • 13

3 Answers3

1

Your issue is you're calling a function called handleTap: that receives a parameter but you don't have a function called handleTap:. That's what action represents in this line:

let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))

Change your func tapped() to:

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Ended {
        print("Menu button released")
    }
}
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
  • Okay I tried this it still causes a crash. let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:") tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)]; self.view!.addGestureRecognizer(tapGesture) func handleTap(sender: UITapGestureRecognizer) { print("button tapped") } – Ferdinand Lösch Apr 24 '16 at 14:15
  • it still seems like the that the selector is not calling the function correctly GameScene handleTap:]: unrecognized selector sent to instance 0x126733890 – Ferdinand Lösch Apr 24 '16 at 14:18
  • It's strangely still not working; I used a different way to do what I wanted, using the touchpad, but I am still getting the same error message. – Ferdinand Lösch Apr 24 '16 at 14:37
  • @FerdinandLösch not a problem. You should post an answer with your solution for future readers. – Daniel Storm Apr 25 '16 at 10:14
1

I use the following for Swift 4 (as per question: @selector() in Swift?)

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)

@objc func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Ended {
        print("Menu button released")
    }
}
norq
  • 1,404
  • 2
  • 18
  • 35
0

I solved my problem by moving the tapRecognizer selector into my previously set up touch handler function so the code looks like this now:

private func handleTouches(touches: Set<UITouch>) {
    for touch in touches {
        let touchLocation = touch.locationInNode(self)
        lastTouch = touchLocation

        let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
        tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)];
        self.view!.addGestureRecognizer(tapRecognizer)
    }
}

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Ended {
        print("Menu button released")
    }     
 }
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Ferdinand Lösch
  • 185
  • 3
  • 13